Fixed NoActivityRelatedPermissionTest failure

The test was previously checking if there was 2 tasks
(1 for the current activity and the other for the home activity)
returned for the getRecentTasks call, however it is possible for
us to have multiple home tasks as some point and the test will
fail. We now filter out all the home tasks from the returned list
before checking that there is 1 or less task in the results.

Bug: 28552936
Change-Id: Ife7db8a9643e2cb7070172b0030ee2bc7063a62f
diff --git a/tests/tests/permission/src/android/permission/cts/NoActivityRelatedPermissionTest.java b/tests/tests/permission/src/android/permission/cts/NoActivityRelatedPermissionTest.java
index 72b5530..bdd5c51 100644
--- a/tests/tests/permission/src/android/permission/cts/NoActivityRelatedPermissionTest.java
+++ b/tests/tests/permission/src/android/permission/cts/NoActivityRelatedPermissionTest.java
@@ -21,6 +21,7 @@
 import android.app.ActivityManager;
 import android.app.AlertDialog;
 import android.content.Context;
+import android.content.Intent;
 import android.test.ActivityInstrumentationTestCase2;
 import android.test.suitebuilder.annotation.MediumTest;
 import android.test.suitebuilder.annotation.Suppress;
@@ -64,8 +65,27 @@
 
         List<ActivityManager.RecentTaskInfo> recentTasks = manager.getRecentTasks(10,
                 ActivityManager.RECENT_WITH_EXCLUDED);
-        // Current implementation should only return tasks for home and the caller.
-        // We'll be done and task this to mean it shouldn't return more than 2.
-        assertTrue("Found tasks: " + recentTasks, recentTasks == null || recentTasks.size() <= 2);
+        // Current implementation should only return tasks for home and the caller. Since there can
+        // be multiple home tasks, we remove them from the list and then check that there is one or
+        // less task left in the list.
+        removeHomeTasks(recentTasks);
+        assertTrue("Found tasks: " + recentTasks, recentTasks == null || recentTasks.size() <= 1);
+    }
+
+    private void removeHomeTasks(List<ActivityManager.RecentTaskInfo> tasks) {
+        for (int i = tasks.size() -1; i >= 0; i--) {
+            ActivityManager.RecentTaskInfo task = tasks.get(i);
+            if (task.baseIntent != null && isHomeIntent(task.baseIntent)) {
+                tasks.remove(i);
+            }
+        }
+    }
+
+    private boolean isHomeIntent(Intent intent) {
+        return Intent.ACTION_MAIN.equals(intent.getAction())
+                && intent.hasCategory(Intent.CATEGORY_HOME)
+                && intent.getCategories().size() == 1
+                && intent.getData() == null
+                && intent.getType() == null;
     }
 }