Don't list apps that are disabled by the system

This is to make sure that apps that were disabled for some reason
such as inappropriateness for a locale or for a specific device, don't
show up as available to enable for a restricted profile.

But in the case that the app was already enabled for the target profile,
show it in the list so that it can be disabled if necessary.

Bug: 10229133
Change-Id: I3c19edb7364cea42d95b619781e0326543b9a1cf
diff --git a/src/com/android/settings/users/AppRestrictionsFragment.java b/src/com/android/settings/users/AppRestrictionsFragment.java
index 0e15cec..edf321a 100644
--- a/src/com/android/settings/users/AppRestrictionsFragment.java
+++ b/src/com/android/settings/users/AppRestrictionsFragment.java
@@ -475,13 +475,24 @@
                 PackageManager.GET_DISABLED_COMPONENTS | PackageManager.GET_UNINSTALLED_PACKAGES);
         for (ResolveInfo app : launchableApps) {
             if (app.activityInfo != null && app.activityInfo.applicationInfo != null) {
+                final String packageName = app.activityInfo.packageName;
                 int flags = app.activityInfo.applicationInfo.flags;
                 if ((flags & ApplicationInfo.FLAG_SYSTEM) != 0
                         || (flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0) {
                     // System app
                     // Skip excluded packages
-                    if (excludePackages.contains(app.activityInfo.packageName)) continue;
-
+                    if (excludePackages.contains(packageName)) continue;
+                    int enabled = pm.getApplicationEnabledSetting(packageName);
+                    if (enabled == PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED
+                            || enabled == PackageManager.COMPONENT_ENABLED_STATE_DISABLED) {
+                        // Check if the app is already enabled for the target user
+                        ApplicationInfo targetUserAppInfo = getAppInfoForUser(packageName,
+                                0, mUser);
+                        if (targetUserAppInfo == null
+                                || (targetUserAppInfo.flags&ApplicationInfo.FLAG_INSTALLED) == 0) {
+                            continue;
+                        }
+                    }
                     SelectableAppInfo info = new SelectableAppInfo();
                     info.packageName = app.activityInfo.packageName;
                     info.appName = app.activityInfo.applicationInfo.loadLabel(pm);
@@ -495,6 +506,16 @@
         }
     }
 
+    private ApplicationInfo getAppInfoForUser(String packageName, int flags, UserHandle user) {
+        try {
+            ApplicationInfo targetUserAppInfo = mIPm.getApplicationInfo(packageName, flags,
+                    user.getIdentifier());
+            return targetUserAppInfo;
+        } catch (RemoteException re) {
+            return null;
+        }
+    }
+
     private class AppLoadingTask extends AsyncTask<Void, Void, Void> {
 
         @Override
@@ -563,6 +584,7 @@
             }
         }
 
+        // Get the list of apps already installed for the user
         mUserApps = null;
         try {
             mUserApps = ipm.getInstalledApplications(
@@ -586,6 +608,8 @@
                 }
             }
         }
+
+        // Sort the list of visible apps
         Collections.sort(mVisibleApps, new AppLabelComparator());
 
         // Remove dupes