Fix to make POP and Imap more locale safe

Cherry-picks from:
https://android-review.googlesource.com/#/c/42334/
https://android-review.googlesource.com/#/c/42332/

Bug: 7138507

Change-Id: I557c3d8c725cf6dc4f13f227dcec63864ab5db67
diff --git a/src/com/android/email/mail/store/ImapFolder.java b/src/com/android/email/mail/store/ImapFolder.java
index 5564223..5d49b01 100644
--- a/src/com/android/email/mail/store/ImapFolder.java
+++ b/src/com/android/email/mail/store/ImapFolder.java
@@ -757,7 +757,7 @@
 
                 } else {
                     if (e.isString()) {
-                        mp.setSubType(bs.getStringOrEmpty(i).getString().toLowerCase());
+                        mp.setSubType(bs.getStringOrEmpty(i).getString().toLowerCase(Locale.US));
                     }
                     break; // Ignore the rest of the list.
                 }
@@ -782,7 +782,7 @@
             final ImapString type = bs.getStringOrEmpty(0);
             final ImapString subType = bs.getStringOrEmpty(1);
             final String mimeType =
-                    (type.getString() + "/" + subType.getString()).toLowerCase();
+                    (type.getString() + "/" + subType.getString()).toLowerCase(Locale.US);
 
             final ImapList bodyParams = bs.getListOrEmpty(2);
             final ImapString cid = bs.getStringOrEmpty(3);
@@ -840,7 +840,7 @@
 
             if (bodyDisposition.size() > 0) {
                 final String bodyDisposition0Str =
-                        bodyDisposition.getStringOrEmpty(0).getString().toLowerCase();
+                        bodyDisposition.getStringOrEmpty(0).getString().toLowerCase(Locale.US);
                 if (!TextUtils.isEmpty(bodyDisposition0Str)) {
                     contentDisposition.append(bodyDisposition0Str);
                 }
@@ -854,9 +854,9 @@
                     for (int i = 1, count = bodyDispositionParams.size(); i < count; i += 2) {
 
                         // TODO We need to convert " into %22.  See above.
-                        contentDisposition.append(String.format(";\n %s=\"%s\"",
+                        contentDisposition.append(String.format(Locale.US, ";\n %s=\"%s\"",
                                 bodyDispositionParams.getStringOrEmpty(i - 1)
-                                        .getString().toLowerCase(),
+                                        .getString().toLowerCase(Locale.US),
                                 bodyDispositionParams.getStringOrEmpty(i).getString()));
                     }
                 }
@@ -865,7 +865,7 @@
             if ((size > 0)
                     && (MimeUtility.getHeaderParameter(contentDisposition.toString(), "size")
                             == null)) {
-                contentDisposition.append(String.format(";\n size=%d", size));
+                contentDisposition.append(String.format(Locale.US, ";\n size=%d", size));
             }
 
             if (contentDisposition.length() > 0) {
diff --git a/src/com/android/email/mail/store/Pop3Store.java b/src/com/android/email/mail/store/Pop3Store.java
index b3ee420..407d47e 100644
--- a/src/com/android/email/mail/store/Pop3Store.java
+++ b/src/com/android/email/mail/store/Pop3Store.java
@@ -48,6 +48,7 @@
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.HashSet;
+import java.util.Locale;
 
 public class Pop3Store extends Store {
     // All flags defining debug or development code settings must be FALSE
@@ -786,13 +787,19 @@
             int messageId = mUidToMsgNumMap.get(message.getUid());
             if (lines == -1) {
                 // Fetch entire message
-                response = executeSimpleCommand(String.format("RETR %d", messageId));
+                response = executeSimpleCommand(String.format(Locale.US, "RETR %d", messageId));
             } else {
                 // Fetch partial message.  Try "TOP", and fall back to slower "RETR" if necessary
                 try {
-                    response = executeSimpleCommand(String.format("TOP %d %d", messageId,  lines));
+                    response = executeSimpleCommand(
+                            String.format(Locale.US, "TOP %d %d", messageId,  lines));
                 } catch (MessagingException me) {
-                    response = executeSimpleCommand(String.format("RETR %d", messageId));
+                    try {
+                        response = executeSimpleCommand(
+                                String.format(Locale.US, "RETR %d", messageId));
+                    } catch (MessagingException e) {
+                        Log.w(Logging.LOG_TAG, "Can't read message " + messageId);
+                    }
                 }
             }
             if (response != null)  {
@@ -846,8 +853,16 @@
             }
             try {
                 for (Message message : messages) {
-                    executeSimpleCommand(String.format("DELE %s",
-                            mUidToMsgNumMap.get(message.getUid())));
+                    try {
+                        final String uid = message.getUid();
+                        final int msgNum = mUidToMsgNumMap.get(uid);
+                        executeSimpleCommand(String.format(Locale.US, "DELE %s", msgNum));
+                        // Remove from the maps
+                        mMsgNumToMsgMap.remove(msgNum);
+                        mUidToMsgNumMap.remove(uid);
+                    } catch (MessagingException e) {
+                        // A failed deletion isn't a problem
+                    }
                 }
             }
             catch (IOException ioe) {