DO NOT MERGE: Don't spam Email with attachment load progress

* I observe that the read() can return with as little as 100
  bytes read; this can lead to lots of spamming via the binder
  interface.
* Limit callbacks based on both percentage read and bytes read
* Clean backport of I25a20279c065bfc1b22c5e2633fc465109cca495

Bug: 5433427
Change-Id: Ic9ec9e558bbff862cd7b2fd6ae24586e695729d9
diff --git a/src/com/android/exchange/adapter/AttachmentLoader.java b/src/com/android/exchange/adapter/AttachmentLoader.java
index 4370349..4d14934 100644
--- a/src/com/android/exchange/adapter/AttachmentLoader.java
+++ b/src/com/android/exchange/adapter/AttachmentLoader.java
@@ -112,6 +112,8 @@
         // Loop terminates 1) when EOF is reached or 2) IOException occurs
         // One of these is guaranteed to occur
         int totalRead = 0;
+        int lastCallbackPct = -1;
+        int lastCallbackTotalRead = 0;
         mService.userLog("Expected attachment length: ", len);
         while (true) {
             int read = inputStream.read(bytes, 0, CHUNK_SIZE);
@@ -128,8 +130,15 @@
 
             // We can't report percentage if data is chunked; the length of incoming data is unknown
             if (length > 0) {
-                // Report progress back to the UI
-                doProgressCallback((totalRead * 100) / length);
+                int pct = (totalRead * 100) / length;
+                // Callback only if we've read at least 1% more and have read more than CHUNK_SIZE
+                // We don't want to spam the Email app
+                if ((pct > lastCallbackPct) && (totalRead > (lastCallbackTotalRead + CHUNK_SIZE))) {
+                    // Report progress back to the UI
+                    doProgressCallback(pct);
+                    lastCallbackTotalRead = totalRead;
+                    lastCallbackPct = pct;
+                }
             }
         }
         if (totalRead > length) {