CardEmulationManager filters preinstalled apps

CardEmulationManager will now filter for preinstalled apps so that only
pre-installed apps could be the default payment provider for the device
initially.

Test: Manual test
Bug: 212610736
(cherry picked from commit 93a78fc1a525a3d8d60e33caf446307044241770)
(cherry picked from https://googleplex-android-review.googlesource.com/q/commit:ee659be677b0eb162912c42871f41ac9f59c4ef0)
Merged-In: Id062cdd685b066bab44dc48bfa57cc97ccc02b35
Change-Id: Id062cdd685b066bab44dc48bfa57cc97ccc02b35
diff --git a/src/com/android/nfc/cardemulation/CardEmulationManager.java b/src/com/android/nfc/cardemulation/CardEmulationManager.java
index 75c08c4..a6f868c 100644
--- a/src/com/android/nfc/cardemulation/CardEmulationManager.java
+++ b/src/com/android/nfc/cardemulation/CardEmulationManager.java
@@ -17,6 +17,9 @@
 
 import android.content.ComponentName;
 import android.content.Context;
+import android.content.pm.ApplicationInfo;
+import android.content.pm.PackageManager;
+import android.content.pm.PackageManager.NameNotFoundException;
 import android.nfc.INfcCardEmulation;
 import android.nfc.INfcFCardEmulation;
 import android.nfc.NfcAdapter;
@@ -294,14 +297,23 @@
                         + userIdDefaultPaymentService);
             }
         }
-
         if (defaultPaymentService == null) {
             // A payment service may have been removed, leaving only one;
             // in that case, automatically set that app as default.
             int numPaymentServices = 0;
             ComponentName lastFoundPaymentService = null;
+            PackageManager pm;
+            try {
+                pm = mContext.createPackageContextAsUser("android", /*flags=*/0,
+                    new UserHandle(userId)).getPackageManager();
+            } catch (NameNotFoundException e) {
+                Log.e(TAG, "Could not create user package context");
+                return;
+            }
+
             for (ApduServiceInfo service : services) {
-                if (service.hasCategory(CardEmulation.CATEGORY_PAYMENT))  {
+                if (service.hasCategory(CardEmulation.CATEGORY_PAYMENT)
+                            && wasServicePreInstalled(pm, service.getComponent())) {
                     numPaymentServices++;
                     lastFoundPaymentService = service.getComponent();
                 }
@@ -323,6 +335,22 @@
         }
     }
 
+    boolean wasServicePreInstalled(PackageManager packageManager, ComponentName service) {
+        try {
+            ApplicationInfo ai = packageManager
+                    .getApplicationInfo(service.getPackageName(), /*flags=*/0);
+            if ((ApplicationInfo.FLAG_SYSTEM & ai.flags) != 0) {
+                if (DBG) Log.d(TAG, "Service was pre-installed on the device");
+                return true;
+            }
+        } catch (NameNotFoundException e) {
+            Log.e(TAG, "Service is not currently installed on the device.");
+            return false;
+        }
+        if (DBG) Log.d(TAG, "Service was not pre-installed on the device");
+        return false;
+    }
+
     ComponentName getDefaultServiceForCategory(int userId, String category,
              boolean validateInstalled) {
         if (!CardEmulation.CATEGORY_PAYMENT.equals(category)) {