Correct parsing of application/profileable tag + cts.

Bug: 160244186
Test: atest PackageManagerShellCommandIncrementalTest
Change-Id: I60de8c5300818a2c14c8532b29a023d1b0e0f5b5
diff --git a/core/java/android/content/pm/PackageParser.java b/core/java/android/content/pm/PackageParser.java
index 5ca55c7..72246ee 100644
--- a/core/java/android/content/pm/PackageParser.java
+++ b/core/java/android/content/pm/PackageParser.java
@@ -1704,13 +1704,6 @@
                         minSdkVersion = attrs.getAttributeIntValue(i, DEFAULT_MIN_SDK_VERSION);
                     }
                 }
-            } else if (TAG_PROFILEABLE.equals(parser.getName())) {
-                for (int i = 0; i < attrs.getAttributeCount(); ++i) {
-                    final String attr = attrs.getAttributeName(i);
-                    if ("shell".equals(attr)) {
-                        profilableByShell = attrs.getAttributeBooleanValue(i, profilableByShell);
-                    }
-                }
             }
         }
 
diff --git a/core/java/android/content/pm/parsing/ApkLiteParseUtils.java b/core/java/android/content/pm/parsing/ApkLiteParseUtils.java
index 4800f89..4914601 100644
--- a/core/java/android/content/pm/parsing/ApkLiteParseUtils.java
+++ b/core/java/android/content/pm/parsing/ApkLiteParseUtils.java
@@ -398,6 +398,30 @@
                             break;
                     }
                 }
+
+                final int innerDepth = parser.getDepth();
+                int innerType;
+                while ((innerType = parser.next()) != XmlPullParser.END_DOCUMENT
+                        && (innerType != XmlPullParser.END_TAG || parser.getDepth() > innerDepth)) {
+                    if (innerType == XmlPullParser.END_TAG || innerType == XmlPullParser.TEXT) {
+                        continue;
+                    }
+
+                    if (parser.getDepth() != innerDepth + 1) {
+                        // Search only under <application>.
+                        continue;
+                    }
+
+                    if (PackageParser.TAG_PROFILEABLE.equals(parser.getName())) {
+                        for (int i = 0; i < attrs.getAttributeCount(); ++i) {
+                            final String attr = attrs.getAttributeName(i);
+                            if ("shell".equals(attr)) {
+                                profilableByShell = attrs.getAttributeBooleanValue(i,
+                                        profilableByShell);
+                            }
+                        }
+                    }
+                }
             } else if (PackageParser.TAG_OVERLAY.equals(parser.getName())) {
                 for (int i = 0; i < attrs.getAttributeCount(); ++i) {
                     final String attr = attrs.getAttributeName(i);
@@ -435,13 +459,6 @@
                         minSdkVersion = attrs.getAttributeIntValue(i, DEFAULT_MIN_SDK_VERSION);
                     }
                 }
-            } else if (PackageParser.TAG_PROFILEABLE.equals(parser.getName())) {
-                for (int i = 0; i < attrs.getAttributeCount(); ++i) {
-                    final String attr = attrs.getAttributeName(i);
-                    if ("shell".equals(attr)) {
-                        profilableByShell = attrs.getAttributeBooleanValue(i, profilableByShell);
-                    }
-                }
             }
         }