Merge "Revert "Revert "AOSP/Email - Bump targetSdkVersion to 28."""
diff --git a/Android.mk b/Android.mk
index c354471..41b04fd 100644
--- a/Android.mk
+++ b/Android.mk
@@ -38,13 +38,13 @@
 
 LOCAL_AAPT_FLAGS := --auto-add-overlay
 LOCAL_AAPT_FLAGS += --extra-packages com.android.mail:com.android.email:com.android.emailcommon
+LOCAL_AAPT_FLAGS += --legacy
 
 LOCAL_STATIC_ANDROID_LIBRARIES := \
     com.android.emailcommon \
     libchips \
     libphotoviewer_appcompat \
     android-opt-bitmap \
-    android-opt-datetimepicker \
     androidx.core_core \
     androidx.media_media \
     androidx.legacy_legacy-support-core-utils \
@@ -67,16 +67,13 @@
 LOCAL_PRODUCT_MODULE := true
 
 LOCAL_PROGUARD_FLAG_FILES := proguard.flags $(unified_email_dir)/proguard.flags
-ifeq (eng,$(TARGET_BUILD_VARIANT))
-  LOCAL_PROGUARD_FLAG_FILES += proguard-test.flags
-endif
+LOCAL_PROGUARD_FLAG_FILES += proguard-test.flags
 
 LOCAL_SDK_VERSION := current
 
+LOCAL_COMPATIBILITY_SUITE := general-tests
+
 include $(BUILD_PACKAGE)
 
-# only include rules to build other stuff for the original package, not the derived package.
-ifeq ($(strip $(LOCAL_PACKAGE_OVERRIDES)),)
 # additionally, build unit tests in a separate .apk
 include $(call all-makefiles-under,$(LOCAL_PATH))
-endif
diff --git a/TEST_MAPPING b/TEST_MAPPING
new file mode 100644
index 0000000..9d32044
--- /dev/null
+++ b/TEST_MAPPING
@@ -0,0 +1,7 @@
+{
+  "presubmit": [
+    {
+      "name": "EmailTests"
+    }
+  ]
+}
diff --git a/src/com/android/email/activity/ComposeActivityEmailExternal.java b/src/com/android/email/activity/ComposeActivityEmailExternal.java
index 455193b..c4147b0 100644
--- a/src/com/android/email/activity/ComposeActivityEmailExternal.java
+++ b/src/com/android/email/activity/ComposeActivityEmailExternal.java
@@ -16,11 +16,22 @@
 
 package com.android.email.activity;
 
+import android.content.Intent;
+import android.os.Bundle;
+import androidx.annotation.Nullable;
+import com.android.mail.compose.ComposeActivity;
+
 /**
  * A subclass of {@link ComposeActivityEmail} which is exported for other Android packages to open.
  */
 public class ComposeActivityEmailExternal extends ComposeActivityEmail {
 
+  @Override
+  protected void onCreate(@Nullable Bundle savedInstanceState) {
+    sanitizeIntent();
+    super.onCreate(savedInstanceState);
+  }
+
   /**
    * Only relevant when WebView Compose is enabled. Change this when WebView
    * Compose is enabled for Email.
@@ -29,4 +40,33 @@
   public boolean isExternal() {
       return false;
   }
+
+  /**
+   * Overrides the value of {@code #getIntent()} so any future callers will get a sanitized version
+   * of the intent.
+   */
+  // See b/114493057 for context.
+  private void sanitizeIntent() {
+    Intent sanitizedIntent = getIntent();
+    if (sanitizedIntent != null) {
+      Bundle originalExtras = sanitizedIntent.getExtras();
+       if (originalExtras == null) {
+        originalExtras = new Bundle();
+      }
+      sanitizedIntent.replaceExtras(new Bundle());
+      copyStringExtraIfExists(ComposeActivity.EXTRA_SUBJECT, originalExtras, sanitizedIntent);
+      copyStringExtraIfExists(ComposeActivity.EXTRA_TO, originalExtras, sanitizedIntent);
+      copyStringExtraIfExists(ComposeActivity.EXTRA_CC, originalExtras, sanitizedIntent);
+      copyStringExtraIfExists(ComposeActivity.EXTRA_BCC, originalExtras, sanitizedIntent);
+      copyStringExtraIfExists(ComposeActivity.EXTRA_BODY, originalExtras, sanitizedIntent);
+      setIntent(sanitizedIntent);
+    }
+  }
+
+  private void copyStringExtraIfExists(
+      String extraKey, Bundle originalExtras, Intent sanitizedIntent) {
+    if (originalExtras.containsKey(extraKey)) {
+      sanitizedIntent.putExtra(extraKey, originalExtras.getString(extraKey));
+    }
+  }
 }
diff --git a/tests/Android.mk b/tests/Android.mk
index 5598c2e..60105f0 100644
--- a/tests/Android.mk
+++ b/tests/Android.mk
@@ -24,13 +24,14 @@
 # We only want this apk build for tests.
 LOCAL_MODULE_TAGS := tests
 
-LOCAL_STATIC_JAVA_LIBRARIES := android-support-test
+LOCAL_STATIC_JAVA_LIBRARIES := androidx.test.rules
 
 # For LOCAL_JAVA_LANGUAGE_VERSION >= 1.7, OpenJDK 9 javac generates synthetic calls to
 # Objects.requireNonNull() which was only added in Android API level 19. Thus, this must
 # stay at 1.6 as long as LOCAL_SDK_VERSION is set to a value < 19. See http://b/38495704
 LOCAL_JAVA_LANGUAGE_VERSION := 1.6
 LOCAL_SDK_VERSION := 14
+LOCAL_COMPATIBILITY_SUITE := general-tests
 LOCAL_PACKAGE_NAME := EmailTests
 LOCAL_INSTRUMENTATION_FOR := Email
 
diff --git a/tests/AndroidManifest.xml b/tests/AndroidManifest.xml
index 8169f68..81cb67e 100644
--- a/tests/AndroidManifest.xml
+++ b/tests/AndroidManifest.xml
@@ -4,9 +4,9 @@
      Licensed under the Apache License, Version 2.0 (the "License");
      you may not use this file except in compliance with the License.
      You may obtain a copy of the License at
-  
+
           http://www.apache.org/licenses/LICENSE-2.0
-  
+
      Unless required by applicable law or agreed to in writing, software
      distributed under the License is distributed on an "AS IS" BASIS,
      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -20,7 +20,7 @@
 
     <!-- We add an application tag here just so that we can indicate that
          this package needs to link against the android.test library,
-         which is needed when building test cases. -->    
+         which is needed when building test cases. -->
     <application>
         <uses-library android:name="android.test.runner" />
     </application>
@@ -28,9 +28,9 @@
     <!--
     This declares that this app uses the instrumentation test runner targeting
     the package of com.android.email.  To run the tests use the command:
-    "adb shell am instrument -w com.android.email.tests/android.support.test.runner.AndroidJUnitRunner"
+    "adb shell am instrument -w com.android.email.tests"
     -->
-    <instrumentation android:name="android.support.test.runner.AndroidJUnitRunner"
+    <instrumentation android:name="androidx.test.runner.AndroidJUnitRunner"
                      android:targetPackage="com.android.email"
                      android:label="Tests for Email."/>
 
diff --git a/tests/AndroidTest.xml b/tests/AndroidTest.xml
new file mode 100644
index 0000000..8cced70
--- /dev/null
+++ b/tests/AndroidTest.xml
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Copyright (C) 2019 The Android Open Source Project
+
+     Licensed under the Apache License, Version 2.0 (the "License");
+     you may not use this file except in compliance with the License.
+     You may obtain a copy of the License at
+
+          http://www.apache.org/licenses/LICENSE-2.0
+
+     Unless required by applicable law or agreed to in writing, software
+     distributed under the License is distributed on an "AS IS" BASIS,
+     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+     See the License for the specific language governing permissions and
+     limitations under the License.
+-->
+<configuration description="Runs Email Tests.">
+    <option name="test-suite-tag" value="apct" />
+    <target_preparer class="com.android.tradefed.targetprep.TestAppInstallSetup">
+        <option name="cleanup-apks" value="true" />
+        <option name="test-file-name" value="EmailTests.apk" />
+        <option name="test-file-name" value="Email.apk" />
+    </target_preparer>
+
+    <test class="com.android.tradefed.testtype.AndroidJUnitTest" >
+        <option name="runner" value="androidx.test.runner.AndroidJUnitRunner" />
+        <option name="package" value="com.android.email.tests" />
+    </test>
+</configuration>
diff --git a/tests/src/com/android/email/DummyFlakyTest.java b/tests/src/com/android/email/DummyFlakyTest.java
index a7278b6..99e37b4 100644
--- a/tests/src/com/android/email/DummyFlakyTest.java
+++ b/tests/src/com/android/email/DummyFlakyTest.java
@@ -16,7 +16,8 @@
 
 package com.android.email;
 
-import android.support.test.filters.FlakyTest;
+import androidx.test.filters.FlakyTest;
+
 import org.junit.Test;
 
 /**