Show attachments size during composing MMS

It shows attachments size during composing MMS.
Note: Images could be resized before sending.

Test: Manual

Change-Id: I63eafe0d57259a7742ab839c4d96d595675e70e9
Signed-off-by: Taesu Lee <taesu82.lee@samsung.com>
diff --git a/res/layout/compose_message_view.xml b/res/layout/compose_message_view.xml
index fd60e2a..8bb8249 100644
--- a/res/layout/compose_message_view.xml
+++ b/res/layout/compose_message_view.xml
@@ -88,8 +88,8 @@
             android:layout_height="wrap_content"
             android:orientation="horizontal">
 
-            <!-- Contains compose message bubble and character counter for SMS which should be left
-            aligned -->
+            <!-- Contains compose message bubble and character counter for SMS or attachments size
+                 for MMS which should be left aligned -->
             <LinearLayout
                 android:layout_width="0dp"
                 android:layout_weight="1"
@@ -128,8 +128,8 @@
                 </LinearLayout>
 
                 <TextView
-                    android:id="@+id/char_counter"
-                    style="@style/ComposeMessageViewTextCounterStyle"
+                    android:id="@+id/message_body_size"
+                    style="@style/ComposeMessageViewMessageBodySizeStyle"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     android:singleLine="true"
diff --git a/res/values-ldrtl/styles.xml b/res/values-ldrtl/styles.xml
index bd270c7..de4d9e8 100644
--- a/res/values-ldrtl/styles.xml
+++ b/res/values-ldrtl/styles.xml
@@ -171,9 +171,9 @@
         <item name="android:layout_marginStart">4dp</item>
     </style>
 
-    <style name="ComposeMessageViewTextCounterStyle">
-        <item name="android:textColor">@color/message_text_counter_color</item>
-        <item name="android:textSize">@dimen/message_text_counter_size</item>
+    <style name="ComposeMessageViewMessageBodySizeStyle">
+        <item name="android:textColor">@color/message_body_size_text_color</item>
+        <item name="android:textSize">@dimen/message_body_size_text_size</item>
         <item name="android:fontFamily">sans-serif</item>
         <item name="android:layout_gravity">end|center_vertical</item>
         <item name="android:paddingEnd">@dimen/compose_message_text_box_padding_side</item>
diff --git a/res/values/colors.xml b/res/values/colors.xml
index f33e105..035ed81 100644
--- a/res/values/colors.xml
+++ b/res/values/colors.xml
@@ -128,7 +128,7 @@
     <color name="fab_pressed_color">#3ea4dc</color>
     <color name="fab_ripple">#40ffffff</color>
 
-    <color name="message_text_counter_color">#555555</color>
+    <color name="message_body_size_text_color">#555555</color>
     <color name="mms_indicator_color">#8BC34A</color>
     <color name="list_empty_text">#6d6d6d</color>
     <color name="low_storage_action_item_color">#ff000000</color>
diff --git a/res/values/dimens.xml b/res/values/dimens.xml
index 5ff0eb7..5c34b6f 100644
--- a/res/values/dimens.xml
+++ b/res/values/dimens.xml
@@ -116,7 +116,7 @@
     <dimen name="fab_elevation_pressed">6dp</dimen>
     <dimen name="fab_bottom_margin">12dp</dimen>
     <dimen name="fab_left_right_margin">14dp</dimen>
-    <dimen name="message_text_counter_size">12sp</dimen>
+    <dimen name="message_body_size_text_size">12sp</dimen>
 
     <dimen name="vcard_detail_group_indicator_width">40dp</dimen>
     <dimen name="mms_indicator_size">12sp</dimen>
diff --git a/res/values/styles.xml b/res/values/styles.xml
index 442818d..46533af 100644
--- a/res/values/styles.xml
+++ b/res/values/styles.xml
@@ -365,9 +365,9 @@
       <item name="android:dividerHeight">0dp</item>
     </style>
 
-    <style name="ComposeMessageViewTextCounterStyle">
-        <item name="android:textColor">@color/message_text_counter_color</item>
-        <item name="android:textSize">@dimen/message_text_counter_size</item>
+    <style name="ComposeMessageViewMessageBodySizeStyle">
+        <item name="android:textColor">@color/message_body_size_text_color</item>
+        <item name="android:textSize">@dimen/message_body_size_text_size</item>
         <item name="android:fontFamily">sans-serif</item>
         <item name="android:layout_gravity">right|center_vertical</item>
         <item name="android:paddingRight">@dimen/compose_message_text_box_padding_side</item>
diff --git a/src/com/android/messaging/ui/conversation/ComposeMessageView.java b/src/com/android/messaging/ui/conversation/ComposeMessageView.java
index 5db1292..0f36e9a 100644
--- a/src/com/android/messaging/ui/conversation/ComposeMessageView.java
+++ b/src/com/android/messaging/ui/conversation/ComposeMessageView.java
@@ -27,6 +27,7 @@
 import android.text.InputFilter.LengthFilter;
 import android.text.TextUtils;
 import android.text.TextWatcher;
+import android.text.format.Formatter;
 import android.util.AttributeSet;
 import android.view.ContextThemeWrapper;
 import android.view.KeyEvent;
@@ -67,7 +68,9 @@
 import com.android.messaging.util.LogUtil;
 import com.android.messaging.util.MediaUtil;
 import com.android.messaging.util.OsUtil;
+import com.android.messaging.util.SafeAsyncTask;
 import com.android.messaging.util.UiUtils;
+import com.android.messaging.util.UriUtil;
 
 import java.util.Collection;
 import java.util.List;
@@ -112,7 +115,7 @@
 
     private PlainTextEditText mComposeEditText;
     private PlainTextEditText mComposeSubjectText;
-    private TextView mCharCounter;
+    private TextView mMessageBodySize;
     private TextView mMmsIndicator;
     private SimIconView mSelfSendIcon;
     private ImageButton mSendButton;
@@ -171,7 +174,7 @@
 
         final int counterColor = mHost.overrideCounterColor();
         if (counterColor != -1) {
-            mCharCounter.setTextColor(counterColor);
+            mMessageBodySize.setTextColor(counterColor);
         }
     }
 
@@ -309,7 +312,7 @@
         mAttachmentPreview = (AttachmentPreview) findViewById(R.id.attachment_draft_view);
         mAttachmentPreview.setComposeMessageView(this);
 
-        mCharCounter = (TextView) findViewById(R.id.char_counter);
+        mMessageBodySize = (TextView) findViewById(R.id.message_body_size);
         mMmsIndicator = (TextView) findViewById(R.id.mms_indicator);
     }
 
@@ -480,6 +483,8 @@
         final String subject = data.getMessageSubject();
         final String message = data.getMessageText();
 
+        boolean hasAttachmentsChanged = false;
+
         if ((changeFlags & DraftMessageData.MESSAGE_SUBJECT_CHANGED) ==
                 DraftMessageData.MESSAGE_SUBJECT_CHANGED) {
             mComposeSubjectText.setText(subject);
@@ -500,12 +505,13 @@
                 DraftMessageData.ATTACHMENTS_CHANGED) {
             final boolean haveAttachments = mAttachmentPreview.onAttachmentsChanged(data);
             mHost.onAttachmentsChanged(haveAttachments);
+            hasAttachmentsChanged = true;
         }
 
         if ((changeFlags & DraftMessageData.SELF_CHANGED) == DraftMessageData.SELF_CHANGED) {
             updateOnSelfSubscriptionChange();
         }
-        updateVisualsOnDraftChanged();
+        updateVisualsOnDraftChanged(hasAttachmentsChanged);
     }
 
     @Override   // From DraftMessageDataListener
@@ -624,7 +630,44 @@
                 mConversationDataModel.getData().getParticipantsLoaded();
     }
 
+    private static class AsyncUpdateMessageBodySizeTask
+            extends SafeAsyncTask<List<MessagePartData>, Void, Long> {
+
+        private final Context mContext;
+        private final TextView mSizeTextView;
+
+        public AsyncUpdateMessageBodySizeTask(final Context context, final TextView tv) {
+            mContext = context;
+            mSizeTextView = tv;
+        }
+
+        @Override
+        protected Long doInBackgroundTimed(final List<MessagePartData>... params) {
+            final List<MessagePartData> attachments = params[0];
+            long totalSize = 0;
+            for (final MessagePartData attachment : attachments) {
+                final Uri contentUri = attachment.getContentUri();
+                if (contentUri != null) {
+                    totalSize += UriUtil.getContentSize(attachment.getContentUri());
+                }
+            }
+            return totalSize;
+        }
+
+        @Override
+        protected void onPostExecute(Long size) {
+            if (mSizeTextView != null) {
+                mSizeTextView.setText(Formatter.formatFileSize(mContext, size));
+                mSizeTextView.setVisibility(View.VISIBLE);
+            }
+        }
+    }
+
     private void updateVisualsOnDraftChanged() {
+        updateVisualsOnDraftChanged(false);
+    }
+
+    private void updateVisualsOnDraftChanged(boolean hasAttachmentsChanged) {
         final String messageText = mComposeEditText.getText().toString();
         final DraftMessageData draftMessageData = mBinding.getData();
         draftMessageData.setMessageText(messageText);
@@ -640,26 +683,39 @@
         final boolean hasWorkingDraft = hasMessageText || hasSubject ||
                 mBinding.getData().hasAttachments();
 
-        // Update the SMS text counter.
-        final int messageCount = draftMessageData.getNumMessagesToBeSent();
-        final int codePointsRemaining = draftMessageData.getCodePointsRemainingInCurrentMessage();
-        // Show the counter only if:
-        // - We are not in MMS mode
-        // - We are going to send more than one message OR we are getting close
-        boolean showCounter = false;
-        if (!draftMessageData.getIsMms() && (messageCount > 1 ||
-                 codePointsRemaining <= CODEPOINTS_REMAINING_BEFORE_COUNTER_SHOWN)) {
-            showCounter = true;
-        }
-
-        if (showCounter) {
-            // Update the remaining characters and number of messages required.
-            final String counterText = messageCount > 1 ? codePointsRemaining + " / " +
-                    messageCount : String.valueOf(codePointsRemaining);
-            mCharCounter.setText(counterText);
-            mCharCounter.setVisibility(View.VISIBLE);
-        } else {
-            mCharCounter.setVisibility(View.INVISIBLE);
+        final List<MessagePartData> attachments = draftMessageData.getReadOnlyAttachments();
+        if (draftMessageData.getIsMms()) { // MMS case
+            if (draftMessageData.hasAttachments()) {
+                if (hasAttachmentsChanged) {
+                    // Calculate message attachments size and show it.
+                    new AsyncUpdateMessageBodySizeTask(getContext(), mMessageBodySize)
+                            .executeOnThreadPool(attachments, null, null);
+                } else {
+                    // No update. Just show previous size.
+                    mMessageBodySize.setVisibility(View.VISIBLE);
+                }
+            } else {
+                mMessageBodySize.setVisibility(View.INVISIBLE);
+            }
+        } else { // SMS case
+            // Update the SMS text counter.
+            final int messageCount = draftMessageData.getNumMessagesToBeSent();
+            final int codePointsRemaining =
+                    draftMessageData.getCodePointsRemainingInCurrentMessage();
+            // Show the counter only if we are going to send more than one message OR we are getting
+            // close.
+            if (messageCount > 1
+                    || codePointsRemaining <= CODEPOINTS_REMAINING_BEFORE_COUNTER_SHOWN) {
+                // Update the remaining characters and number of messages required.
+                final String counterText =
+                        messageCount > 1
+                                ? codePointsRemaining + " / " + messageCount
+                                : String.valueOf(codePointsRemaining);
+                mMessageBodySize.setText(counterText);
+                mMessageBodySize.setVisibility(View.VISIBLE);
+            } else {
+                mMessageBodySize.setVisibility(View.INVISIBLE);
+            }
         }
 
         // Update the send message button. Self icon uri might be null if self participant data
@@ -699,7 +755,6 @@
         }
 
         // Update the text hint on the message box depending on the attachment type.
-        final List<MessagePartData> attachments = draftMessageData.getReadOnlyAttachments();
         final int attachmentCount = attachments.size();
         if (attachmentCount == 0) {
             final SubscriptionListEntry subscriptionListEntry =