Merge "Grid recents: tweak task header layout to better match mocks"
diff --git a/api/system-current.txt b/api/system-current.txt
index da15176..1ced79c 100644
--- a/api/system-current.txt
+++ b/api/system-current.txt
@@ -32395,6 +32395,7 @@
     field public static final java.lang.String DISALLOW_MODIFY_ACCOUNTS = "no_modify_accounts";
     field public static final java.lang.String DISALLOW_MOUNT_PHYSICAL_MEDIA = "no_physical_media";
     field public static final java.lang.String DISALLOW_NETWORK_RESET = "no_network_reset";
+    field public static final java.lang.String DISALLOW_OEM_UNLOCK = "no_oem_unlock";
     field public static final java.lang.String DISALLOW_OUTGOING_BEAM = "no_outgoing_beam";
     field public static final java.lang.String DISALLOW_OUTGOING_CALLS = "no_outgoing_calls";
     field public static final java.lang.String DISALLOW_REMOVE_USER = "no_remove_user";
diff --git a/core/java/android/os/UserManager.java b/core/java/android/os/UserManager.java
index 0d3b328..2c0b2a0 100644
--- a/core/java/android/os/UserManager.java
+++ b/core/java/android/os/UserManager.java
@@ -634,11 +634,14 @@
      * <code>false</code>. Setting this restriction has no effect if the bootloader is already
      * unlocked.
      *
+     * <p>Not for use by third-party applications.
+     *
      * @see DevicePolicyManager#addUserRestriction(ComponentName, String)
      * @see DevicePolicyManager#clearUserRestriction(ComponentName, String)
      * @see #getUserRestrictions()
      * @hide
      */
+    @SystemApi
     public static final String DISALLOW_OEM_UNLOCK = "no_oem_unlock";
 
     /**
diff --git a/packages/SettingsLib/src/com/android/settingslib/RestrictedLockUtils.java b/packages/SettingsLib/src/com/android/settingslib/RestrictedLockUtils.java
index 6bd8a87..99d7f1e 100644
--- a/packages/SettingsLib/src/com/android/settingslib/RestrictedLockUtils.java
+++ b/packages/SettingsLib/src/com/android/settingslib/RestrictedLockUtils.java
@@ -16,6 +16,7 @@
 
 package com.android.settingslib;
 
+import android.annotation.UserIdInt;
 import android.app.AppGlobals;
 import android.app.admin.DevicePolicyManager;
 import android.content.ComponentName;
@@ -118,21 +119,25 @@
      */
     public static EnforcedAdmin checkIfKeyguardFeaturesDisabled(Context context,
             int keyguardFeatures, int userId) {
+        final LockSettingCheck check =
+                (DevicePolicyManager dpm, ComponentName admin, @UserIdInt int checkUser) ->
+                        (dpm.getKeyguardDisabledFeatures(admin, checkUser) & keyguardFeatures) != 0;
+
         final DevicePolicyManager dpm = (DevicePolicyManager) context.getSystemService(
                 Context.DEVICE_POLICY_SERVICE);
         if (dpm == null) {
             return null;
         }
+
         final UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE);
-        LockPatternUtils lockPatternUtils = new LockPatternUtils(context);
-        EnforcedAdmin enforcedAdmin = null;
         if (um.getUserInfo(userId).isManagedProfile()) {
             final List<ComponentName> admins = dpm.getActiveAdminsAsUser(userId);
             if (admins == null) {
                 return null;
             }
+            EnforcedAdmin enforcedAdmin = null;
             for (ComponentName admin : admins) {
-                if ((dpm.getKeyguardDisabledFeatures(admin, userId) & keyguardFeatures) != 0) {
+                if (check.isEnforcing(dpm, admin, userId)) {
                     if (enforcedAdmin == null) {
                         enforcedAdmin = new EnforcedAdmin(admin, userId);
                     } else {
@@ -140,49 +145,10 @@
                     }
                 }
             }
+            return enforcedAdmin;
         } else {
-            // Consider all admins for this user and the profiles that are visible from this
-            // user that do not use a separate work challenge.
-            for (UserInfo userInfo : um.getProfiles(userId)) {
-                final List<ComponentName> admins = dpm.getActiveAdminsAsUser(userInfo.id);
-                if (admins == null) {
-                    continue;
-                }
-                final boolean isSeparateProfileChallengeEnabled =
-                        lockPatternUtils.isSeparateProfileChallengeEnabled(userInfo.id);
-                for (ComponentName admin : admins) {
-                    if (!isSeparateProfileChallengeEnabled) {
-                        if ((dpm.getKeyguardDisabledFeatures(admin, userInfo.id)
-                                    & keyguardFeatures) != 0) {
-                            if (enforcedAdmin == null) {
-                                enforcedAdmin = new EnforcedAdmin(admin, userInfo.id);
-                            } else {
-                                return EnforcedAdmin.MULTIPLE_ENFORCED_ADMIN;
-                            }
-                            // This same admins could have set policies both on the managed profile
-                            // and on the parent. So, if the admin has set the policy on the
-                            // managed profile here, we don't need to further check if that admin
-                            // has set policy on the parent admin.
-                            continue;
-                        }
-                    }
-                    if (userInfo.isManagedProfile()) {
-                        // If userInfo.id is a managed profile, we also need to look at
-                        // the policies set on the parent.
-                        DevicePolicyManager parentDpm = dpm.getParentProfileInstance(userInfo);
-                        if ((parentDpm.getKeyguardDisabledFeatures(admin, userInfo.id)
-                                & keyguardFeatures) != 0) {
-                            if (enforcedAdmin == null) {
-                                enforcedAdmin = new EnforcedAdmin(admin, userInfo.id);
-                            } else {
-                                return EnforcedAdmin.MULTIPLE_ENFORCED_ADMIN;
-                            }
-                        }
-                    }
-                }
-            }
+            return checkForLockSetting(context, userId, check);
         }
-        return enforcedAdmin;
     }
 
     public static EnforcedAdmin checkIfUninstallBlocked(Context context,
@@ -383,6 +349,11 @@
      *
      */
     public static EnforcedAdmin checkIfPasswordQualityIsSet(Context context, int userId) {
+        final LockSettingCheck check =
+                (DevicePolicyManager dpm, ComponentName admin, @UserIdInt int checkUser) ->
+                        dpm.getPasswordQuality(admin, checkUser)
+                                > DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED;
+
         final DevicePolicyManager dpm = (DevicePolicyManager) context.getSystemService(
                 Context.DEVICE_POLICY_SERVICE);
         if (dpm == null) {
@@ -390,7 +361,6 @@
         }
 
         LockPatternUtils lockPatternUtils = new LockPatternUtils(context);
-        EnforcedAdmin enforcedAdmin = null;
         if (lockPatternUtils.isSeparateProfileChallengeEnabled(userId)) {
             // userId is managed profile and has a separate challenge, only consider
             // the admins in that user.
@@ -398,9 +368,9 @@
             if (admins == null) {
                 return null;
             }
+            EnforcedAdmin enforcedAdmin = null;
             for (ComponentName admin : admins) {
-                if (dpm.getPasswordQuality(admin, userId)
-                        > DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED) {
+                if (check.isEnforcing(dpm, admin, userId)) {
                     if (enforcedAdmin == null) {
                         enforcedAdmin = new EnforcedAdmin(admin, userId);
                     } else {
@@ -408,50 +378,10 @@
                     }
                 }
             }
+            return enforcedAdmin;
         } else {
-            // Return all admins for this user and the profiles that are visible from this
-            // user that do not use a separate work challenge.
-            final UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE);
-            for (UserInfo userInfo : um.getProfiles(userId)) {
-                final List<ComponentName> admins = dpm.getActiveAdminsAsUser(userInfo.id);
-                if (admins == null) {
-                    continue;
-                }
-                final boolean isSeparateProfileChallengeEnabled =
-                        lockPatternUtils.isSeparateProfileChallengeEnabled(userInfo.id);
-                for (ComponentName admin : admins) {
-                    if (!isSeparateProfileChallengeEnabled) {
-                        if (dpm.getPasswordQuality(admin, userInfo.id)
-                                > DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED) {
-                            if (enforcedAdmin == null) {
-                                enforcedAdmin = new EnforcedAdmin(admin, userInfo.id);
-                            } else {
-                                return EnforcedAdmin.MULTIPLE_ENFORCED_ADMIN;
-                            }
-                            // This same admins could have set policies both on the managed profile
-                            // and on the parent. So, if the admin has set the policy on the
-                            // managed profile here, we don't need to further check if that admin
-                            // has set policy on the parent admin.
-                            continue;
-                        }
-                    }
-                    if (userInfo.isManagedProfile()) {
-                        // If userInfo.id is a managed profile, we also need to look at
-                        // the policies set on the parent.
-                        DevicePolicyManager parentDpm = dpm.getParentProfileInstance(userInfo);
-                        if (parentDpm.getPasswordQuality(admin, userInfo.id)
-                                > DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED) {
-                            if (enforcedAdmin == null) {
-                                enforcedAdmin = new EnforcedAdmin(admin, userInfo.id);
-                            } else {
-                                return EnforcedAdmin.MULTIPLE_ENFORCED_ADMIN;
-                            }
-                        }
-                    }
-                }
-            }
+            return checkForLockSetting(context, userId, check);
         }
-        return enforcedAdmin;
     }
 
     /**
@@ -512,6 +442,65 @@
         return enforcedAdmin;
     }
 
+    private interface LockSettingCheck {
+        boolean isEnforcing(DevicePolicyManager dpm, ComponentName admin, @UserIdInt int userId);
+    }
+
+    /**
+     * Checks whether any of the user's profiles enforce the lock setting. A managed profile is only
+     * included if it does not have a separate challenege but the settings for it's parent (i.e. the
+     * user being checked) are always included.
+     */
+    private static EnforcedAdmin checkForLockSetting(
+            Context context, @UserIdInt int userId, LockSettingCheck check) {
+        final DevicePolicyManager dpm = (DevicePolicyManager) context.getSystemService(
+                Context.DEVICE_POLICY_SERVICE);
+        if (dpm == null) {
+            return null;
+        }
+        final LockPatternUtils lockPatternUtils = new LockPatternUtils(context);
+        EnforcedAdmin enforcedAdmin = null;
+        // Return all admins for this user and the profiles that are visible from this
+        // user that do not use a separate work challenge.
+        for (UserInfo userInfo : UserManager.get(context).getProfiles(userId)) {
+            final List<ComponentName> admins = dpm.getActiveAdminsAsUser(userInfo.id);
+            if (admins == null) {
+                continue;
+            }
+            final boolean isSeparateProfileChallengeEnabled =
+                    lockPatternUtils.isSeparateProfileChallengeEnabled(userInfo.id);
+            for (ComponentName admin : admins) {
+                if (!isSeparateProfileChallengeEnabled) {
+                    if (check.isEnforcing(dpm, admin, userInfo.id)) {
+                        if (enforcedAdmin == null) {
+                            enforcedAdmin = new EnforcedAdmin(admin, userInfo.id);
+                        } else {
+                            return EnforcedAdmin.MULTIPLE_ENFORCED_ADMIN;
+                        }
+                        // This same admins could have set policies both on the managed profile
+                        // and on the parent. So, if the admin has set the policy on the
+                        // managed profile here, we don't need to further check if that admin
+                        // has set policy on the parent admin.
+                        continue;
+                    }
+                }
+                if (userInfo.isManagedProfile()) {
+                    // If userInfo.id is a managed profile, we also need to look at
+                    // the policies set on the parent.
+                    final DevicePolicyManager parentDpm = dpm.getParentProfileInstance(userInfo);
+                    if (check.isEnforcing(parentDpm, admin, userInfo.id)) {
+                        if (enforcedAdmin == null) {
+                            enforcedAdmin = new EnforcedAdmin(admin, userInfo.id);
+                        } else {
+                            return EnforcedAdmin.MULTIPLE_ENFORCED_ADMIN;
+                        }
+                    }
+                }
+            }
+        }
+        return enforcedAdmin;
+    }
+
     public static EnforcedAdmin getProfileOrDeviceOwner(Context context, int userId) {
         if (userId == UserHandle.USER_NULL) {
             return null;
diff --git a/packages/SettingsLib/tests/robotests/src/com/android/settingslib/RestrictedLockUtilsTest.java b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/RestrictedLockUtilsTest.java
new file mode 100644
index 0000000..025bbc2
--- /dev/null
+++ b/packages/SettingsLib/tests/robotests/src/com/android/settingslib/RestrictedLockUtilsTest.java
@@ -0,0 +1,120 @@
+/*
+ * Copyright (C) 2016 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.
+ */
+
+package com.android.settingslib;
+
+import android.app.admin.DevicePolicyManager;
+import android.content.ComponentName;
+import android.content.Context;
+import android.content.pm.UserInfo;
+import android.os.UserManager;
+
+import com.android.internal.util.ArrayUtils;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+import org.robolectric.RobolectricTestRunner;
+import org.robolectric.annotation.Config;
+
+import java.util.Arrays;
+
+import static android.app.admin.DevicePolicyManager.KEYGUARD_DISABLE_FINGERPRINT;
+import static android.app.admin.DevicePolicyManager.KEYGUARD_DISABLE_REMOTE_INPUT;
+import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
+import static com.google.common.truth.Truth.assertThat;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.when;
+
+@RunWith(RobolectricTestRunner.class)
+@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
+public class RestrictedLockUtilsTest {
+
+    @Mock
+    private Context mContext;
+    @Mock
+    private DevicePolicyManager mDevicePolicyManager;
+    @Mock
+    private UserManager mUserManager;
+
+    private static final int mUserId = 194;
+    private static final ComponentName mAdmin1 = new ComponentName("admin1", "admin1class");
+    private static final ComponentName mAdmin2 = new ComponentName("admin2", "admin2class");
+
+    @Before
+    public void setUp() {
+        MockitoAnnotations.initMocks(this);
+
+        when(mContext.getSystemService(Context.DEVICE_POLICY_SERVICE))
+                .thenReturn(mDevicePolicyManager);
+        when(mContext.getSystemService(Context.USER_SERVICE))
+                .thenReturn(mUserManager);
+    }
+
+    @Test
+    public void checkIfKeyguardFeaturesDisabled_noEnforcedAdminForManagedProfile() {
+        setUpManagedProfile(mUserId);
+        setUpActiveAdmins(mUserId, new ComponentName[] {mAdmin1, mAdmin2});
+
+        final EnforcedAdmin enforcedAdmin = RestrictedLockUtils.checkIfKeyguardFeaturesDisabled(
+                mContext, KEYGUARD_DISABLE_FINGERPRINT, mUserId);
+
+        assertThat(enforcedAdmin).isEqualTo(null);
+    }
+
+    @Test
+    public void checkIfKeyguardFeaturesDisabled_oneEnforcedAdminForManagedProfile() {
+        setUpManagedProfile(mUserId);
+        setUpActiveAdmins(mUserId, new ComponentName[] {mAdmin1, mAdmin2});
+
+        when(mDevicePolicyManager.getKeyguardDisabledFeatures(mAdmin1, mUserId))
+                .thenReturn(KEYGUARD_DISABLE_FINGERPRINT);
+
+        final EnforcedAdmin enforcedAdmin = RestrictedLockUtils.checkIfKeyguardFeaturesDisabled(
+                mContext, KEYGUARD_DISABLE_FINGERPRINT, mUserId);
+
+        assertThat(enforcedAdmin).isEqualTo(new EnforcedAdmin(mAdmin1, mUserId));
+    }
+
+    @Test
+    public void checkIfKeyguardFeaturesDisabled_multipleEnforcedAdminForManagedProfile() {
+        setUpManagedProfile(mUserId);
+        setUpActiveAdmins(mUserId, new ComponentName[] {mAdmin1, mAdmin2});
+
+        when(mDevicePolicyManager.getKeyguardDisabledFeatures(mAdmin1, mUserId))
+                .thenReturn(KEYGUARD_DISABLE_REMOTE_INPUT);
+        when(mDevicePolicyManager.getKeyguardDisabledFeatures(mAdmin2, mUserId))
+                .thenReturn(KEYGUARD_DISABLE_REMOTE_INPUT);
+
+        final EnforcedAdmin enforcedAdmin = RestrictedLockUtils.checkIfKeyguardFeaturesDisabled(
+                mContext, KEYGUARD_DISABLE_REMOTE_INPUT, mUserId);
+
+        assertThat(enforcedAdmin).isEqualTo(EnforcedAdmin.MULTIPLE_ENFORCED_ADMIN);
+    }
+
+    private UserInfo setUpManagedProfile(int userId) {
+        final UserInfo userInfo = new UserInfo(userId, "myuser", UserInfo.FLAG_MANAGED_PROFILE);
+        when(mUserManager.getUserInfo(userId)).thenReturn(userInfo);
+        return userInfo;
+    }
+
+    private void setUpActiveAdmins(int userId, ComponentName[] activeAdmins) {
+        when(mDevicePolicyManager.getActiveAdminsAsUser(userId))
+                .thenReturn(Arrays.asList(activeAdmins));
+    }
+}
diff --git a/services/core/java/com/android/server/location/GnssLocationProvider.java b/services/core/java/com/android/server/location/GnssLocationProvider.java
index 54366e6..56ba5bb 100644
--- a/services/core/java/com/android/server/location/GnssLocationProvider.java
+++ b/services/core/java/com/android/server/location/GnssLocationProvider.java
@@ -457,6 +457,7 @@
             if (mDownloadXtraDataPending == STATE_PENDING_NETWORK) {
                 xtraDownloadRequest();
             }
+            sendMessage(UPDATE_NETWORK_STATE, 0 /*arg*/, network);
         }
     };
 
@@ -468,11 +469,6 @@
     private final ConnectivityManager.NetworkCallback mSuplConnectivityCallback =
             new ConnectivityManager.NetworkCallback() {
         @Override
-        public void onAvailable(Network network) {
-            sendMessage(UPDATE_NETWORK_STATE, 0 /*arg*/, network);
-        }
-
-        @Override
         public void onLost(Network network) {
             releaseSuplConnection(GPS_RELEASE_AGPS_DATA_CONN);
         }
diff --git a/services/core/java/com/android/server/notification/NotificationManagerService.java b/services/core/java/com/android/server/notification/NotificationManagerService.java
index cbd33cd..a6fb458 100644
--- a/services/core/java/com/android/server/notification/NotificationManagerService.java
+++ b/services/core/java/com/android/server/notification/NotificationManagerService.java
@@ -3962,7 +3962,7 @@
         }
     }
 
-    private static boolean isUidSystem(int uid) {
+    protected static boolean isUidSystem(int uid) {
         final int appid = UserHandle.getAppId(uid);
         return (appid == Process.SYSTEM_UID || appid == Process.PHONE_UID || uid == 0);
     }
diff --git a/services/core/java/com/android/server/notification/NotificationRecord.java b/services/core/java/com/android/server/notification/NotificationRecord.java
index 4fcc987..a03c4aa 100644
--- a/services/core/java/com/android/server/notification/NotificationRecord.java
+++ b/services/core/java/com/android/server/notification/NotificationRecord.java
@@ -136,6 +136,12 @@
     private boolean isPreChannelsNotification() {
         try {
             if (NotificationChannel.DEFAULT_CHANNEL_ID.equals(getChannel().getId())) {
+                final boolean isSystemNotification =
+                        NotificationManagerService.isUidSystem(sbn.getUid())
+                                || ("android".equals(sbn.getPackageName()));
+                if (isSystemNotification) {
+                    return false;
+                }
                 final ApplicationInfo applicationInfo =
                         mContext.getPackageManager().getApplicationInfoAsUser(sbn.getPackageName(),
                                 0, sbn.getUserId());
diff --git a/services/tests/notification/src/com/android/server/notification/NotificationRecordTest.java b/services/tests/notification/src/com/android/server/notification/NotificationRecordTest.java
index a7d2c04..fc94271f 100644
--- a/services/tests/notification/src/com/android/server/notification/NotificationRecordTest.java
+++ b/services/tests/notification/src/com/android/server/notification/NotificationRecordTest.java
@@ -59,7 +59,7 @@
     @Mock PackageManager mPm;
 
     private final String pkg = "com.android.server.notification";
-    private final int uid = 0;
+    private final int uid = 9583;
     private final String pkg2 = "pkg2";
     private final int uid2 = 1111111;
     private final int id1 = 1;