Add background image for work challenge

If the challenge shown is for a work profile, add the default image and
color to the background of the fragment.

Change-Id: I148c6cd3a835a84c7bac78b020839dfdae4a6c36
diff --git a/src/com/android/settings/ChooseLockGeneric.java b/src/com/android/settings/ChooseLockGeneric.java
index bd46456..3def151 100644
--- a/src/com/android/settings/ChooseLockGeneric.java
+++ b/src/com/android/settings/ChooseLockGeneric.java
@@ -172,7 +172,7 @@
                     !mLockPatternUtils.isSeparateProfileChallengeAllowed(targetUser)) {
                 // Always use parent if explicitely requested or if profile challenge is not
                 // supported
-                mUserId = Utils.getSameOwnerUserId(getContext(), getArguments());
+                mUserId = Utils.getUserIdFromBundle(getContext(), getArguments());
             } else {
                 mUserId = targetUser;
             }
diff --git a/src/com/android/settings/ChooseLockPassword.java b/src/com/android/settings/ChooseLockPassword.java
index 6739475..86b6a52 100644
--- a/src/com/android/settings/ChooseLockPassword.java
+++ b/src/com/android/settings/ChooseLockPassword.java
@@ -238,7 +238,7 @@
                 throw new SecurityException("Fragment contained in wrong activity");
             }
             // Only take this argument into account if it belongs to the current profile.
-            mUserId = Utils.getSameOwnerUserId(getActivity(), intent.getExtras());
+            mUserId = Utils.getUserIdFromBundle(getActivity(), intent.getExtras());
             mRequestedQuality = Math.max(intent.getIntExtra(LockPatternUtils.PASSWORD_TYPE_KEY,
                     mRequestedQuality), mLockPatternUtils.getRequestedPasswordQuality(
                     mUserId));
diff --git a/src/com/android/settings/ChooseLockPattern.java b/src/com/android/settings/ChooseLockPattern.java
index 93ffa95..dbbe08c 100644
--- a/src/com/android/settings/ChooseLockPattern.java
+++ b/src/com/android/settings/ChooseLockPattern.java
@@ -370,7 +370,7 @@
             }
             Intent intent = getActivity().getIntent();
             // Only take this argument into account if it belongs to the current profile.
-            mUserId = Utils.getSameOwnerUserId(getActivity(), intent.getExtras());
+            mUserId = Utils.getUserIdFromBundle(getActivity(), intent.getExtras());
         }
 
         @Override
diff --git a/src/com/android/settings/ChooseLockSettingsHelper.java b/src/com/android/settings/ChooseLockSettingsHelper.java
index b88dea2..70ecdfd 100644
--- a/src/com/android/settings/ChooseLockSettingsHelper.java
+++ b/src/com/android/settings/ChooseLockSettingsHelper.java
@@ -90,7 +90,7 @@
     boolean launchConfirmationActivity(int request, CharSequence title, boolean returnCredentials,
             int userId) {
         return launchConfirmationActivity(request, title, null, null,
-                returnCredentials, false, false, 0, Utils.getSameOwnerUserId(mActivity, userId));
+                returnCredentials, false, false, 0, Utils.enforceSameOwner(mActivity, userId));
     }
 
     /**
@@ -110,7 +110,7 @@
             @Nullable CharSequence header, @Nullable CharSequence description,
             boolean returnCredentials, boolean external) {
         return launchConfirmationActivity(request, title, header, description,
-                returnCredentials, external, false, 0, Utils.getEffectiveUserId(mActivity));
+                returnCredentials, external, false, 0, Utils.getCredentialOwnerUserId(mActivity));
     }
 
     /**
@@ -131,7 +131,7 @@
             @Nullable CharSequence header, @Nullable CharSequence description,
             boolean returnCredentials, boolean external, int userId) {
         return launchConfirmationActivity(request, title, header, description,
-                returnCredentials, external, false, 0, Utils.getSameOwnerUserId(mActivity, userId));
+                returnCredentials, external, false, 0, Utils.enforceSameOwner(mActivity, userId));
     }
 
     /**
@@ -147,7 +147,7 @@
             @Nullable CharSequence header, @Nullable CharSequence description,
             long challenge) {
         return launchConfirmationActivity(request, title, header, description,
-                false, false, true, challenge, Utils.getEffectiveUserId(mActivity));
+                false, false, true, challenge, Utils.getCredentialOwnerUserId(mActivity));
     }
 
     private boolean launchConfirmationActivity(int request, @Nullable CharSequence title,
diff --git a/src/com/android/settings/ConfirmDeviceCredentialActivity.java b/src/com/android/settings/ConfirmDeviceCredentialActivity.java
index c4587eb..28c0515 100644
--- a/src/com/android/settings/ConfirmDeviceCredentialActivity.java
+++ b/src/com/android/settings/ConfirmDeviceCredentialActivity.java
@@ -64,12 +64,12 @@
         Intent intent = getIntent();
         String title = intent.getStringExtra(KeyguardManager.EXTRA_TITLE);
         String details = intent.getStringExtra(KeyguardManager.EXTRA_DESCRIPTION);
-        int userId = Utils.getEffectiveUserId(this);
+        int userId = Utils.getCredentialOwnerUserId(this);
         if (isInternalActivity()) {
-            int givenUserId = intent.getIntExtra(Intent.EXTRA_USER_ID, userId);
-            UserManager userManager = UserManager.get(this);
-            if (userManager.isSameProfileGroup(givenUserId, userId)) {
-                userId = givenUserId;
+            try {
+                userId = Utils.getUserIdFromBundle(this, intent.getExtras());
+            } catch (SecurityException se) {
+                Log.e(TAG, "Invalid intent extra", se);
             }
         }
         ChooseLockSettingsHelper helper = new ChooseLockSettingsHelper(this);
diff --git a/src/com/android/settings/ConfirmDeviceCredentialBaseActivity.java b/src/com/android/settings/ConfirmDeviceCredentialBaseActivity.java
index d9af800..e6dd5e0 100644
--- a/src/com/android/settings/ConfirmDeviceCredentialBaseActivity.java
+++ b/src/com/android/settings/ConfirmDeviceCredentialBaseActivity.java
@@ -18,8 +18,10 @@
 
 import android.app.Fragment;
 import android.app.KeyguardManager;
+import android.content.Intent;
 import android.os.Bundle;
 import android.os.Handler;
+import android.os.UserManager;
 import android.view.MenuItem;
 import android.view.WindowManager;
 
@@ -33,7 +35,12 @@
 
     @Override
     protected void onCreate(Bundle savedState) {
-        if (getIntent().getBooleanExtra(ConfirmDeviceCredentialBaseFragment.DARK_THEME, false)) {
+        int credentialOwnerUserId = Utils.getCredentialOwnerUserId(this,
+                Utils.getUserIdFromBundle(this, getIntent().getExtras()));
+        if (Utils.isManagedProfile(UserManager.get(this), credentialOwnerUserId)) {
+            setTheme(R.style.Theme_ConfirmDeviceCredentialsWork);
+        } else if (getIntent().getBooleanExtra(
+                ConfirmDeviceCredentialBaseFragment.DARK_THEME, false)) {
             setTheme(R.style.Theme_ConfirmDeviceCredentialsDark);
             mDark = true;
         }
diff --git a/src/com/android/settings/ConfirmDeviceCredentialBaseFragment.java b/src/com/android/settings/ConfirmDeviceCredentialBaseFragment.java
index 0a22d45..d1ea8be 100644
--- a/src/com/android/settings/ConfirmDeviceCredentialBaseFragment.java
+++ b/src/com/android/settings/ConfirmDeviceCredentialBaseFragment.java
@@ -24,10 +24,16 @@
 import android.content.Context;
 import android.content.Intent;
 import android.content.IntentSender;
+import android.graphics.Point;
+import android.graphics.PorterDuff;
+import android.graphics.drawable.Drawable;
 import android.os.Bundle;
 import android.os.RemoteException;
+import android.os.UserManager;
 import android.view.View;
+import android.view.ViewGroup;
 import android.widget.Button;
+import android.widget.FrameLayout;
 import android.widget.ImageView;
 import android.widget.TextView;
 
@@ -80,6 +86,14 @@
                 getActivity().finish();
             }
         });
+        int credentialOwnerUserId = Utils.getCredentialOwnerUserId(
+                getActivity(),
+                Utils.getUserIdFromBundle(
+                        getActivity(),
+                        getActivity().getIntent().getExtras()));
+        if (Utils.isManagedProfile(UserManager.get(getActivity()), credentialOwnerUserId)) {
+            setWorkChallengeBackground(view);
+        }
     }
 
     @Override
@@ -155,4 +169,20 @@
             }
         }
     }
+
+    private void setWorkChallengeBackground(View baseView) {
+        ImageView imageView = (ImageView) baseView.findViewById(R.id.background_image);
+        if (imageView != null) {
+            Drawable image = getResources().getDrawable(R.drawable.work_challenge_background);
+            image.setColorFilter(
+                    getResources().getColor(R.color.confirm_device_credential_transparent_black),
+                    PorterDuff.Mode.DARKEN);
+            imageView.setImageDrawable(image);
+            Point screenSize = new Point();
+            getActivity().getWindowManager().getDefaultDisplay().getSize(screenSize);
+            imageView.setLayoutParams(new FrameLayout.LayoutParams(
+                    ViewGroup.LayoutParams.MATCH_PARENT,
+                    screenSize.y));
+        }
+    }
 }
diff --git a/src/com/android/settings/ConfirmLockPassword.java b/src/com/android/settings/ConfirmLockPassword.java
index 53555e0..00a68bf 100644
--- a/src/com/android/settings/ConfirmLockPassword.java
+++ b/src/com/android/settings/ConfirmLockPassword.java
@@ -111,7 +111,7 @@
             mLockPatternUtils = new LockPatternUtils(getActivity());
             Intent intent = getActivity().getIntent();
             // Only take this argument into account if it belongs to the current profile.
-            mEffectiveUserId = Utils.getSameOwnerUserId(getActivity(), intent.getExtras());
+            mEffectiveUserId = Utils.getUserIdFromBundle(getActivity(), intent.getExtras());
         }
 
         @Override
diff --git a/src/com/android/settings/ConfirmLockPattern.java b/src/com/android/settings/ConfirmLockPattern.java
index 6331290..087a0b9 100644
--- a/src/com/android/settings/ConfirmLockPattern.java
+++ b/src/com/android/settings/ConfirmLockPattern.java
@@ -116,7 +116,7 @@
             mLockPatternUtils = new LockPatternUtils(getActivity());
             Intent intent = getActivity().getIntent();
             // Only take this argument into account if it belongs to the current profile.
-            mEffectiveUserId = Utils.getSameOwnerUserId(getActivity(), intent.getExtras());
+            mEffectiveUserId = Utils.getUserIdFromBundle(getActivity(), intent.getExtras());
         }
 
         @Override
diff --git a/src/com/android/settings/Utils.java b/src/com/android/settings/Utils.java
index 4afa1d9..56795ae 100644
--- a/src/com/android/settings/Utils.java
+++ b/src/com/android/settings/Utils.java
@@ -16,6 +16,7 @@
 
 package com.android.settings;
 
+import android.annotation.NonNull;
 import android.annotation.Nullable;
 import android.app.ActivityManager;
 import android.app.ActivityManagerNative;
@@ -181,6 +182,19 @@
     }
 
     /**
+     * Returns the UserManager for a given context
+     *
+     * @throws IllegalStateException if no UserManager could be retrieved.
+     */
+    public static UserManager getUserManager(Context context) {
+        UserManager um = UserManager.get(context);
+        if (um == null) {
+            throw new IllegalStateException("Unable to load UserManager");
+        }
+        return um;
+    }
+
+    /**
      * Returns true if Monkey is running.
      */
     public static boolean isMonkeyRunning() {
@@ -569,10 +583,24 @@
 
     /**
      * Returns true if the current profile is a managed one.
+     *
+     * @throws IllegalArgumentException if userManager is null.
      */
-    public static boolean isManagedProfile(UserManager userManager) {
-        UserInfo currentUser = userManager.getUserInfo(userManager.getUserHandle());
-        return currentUser.isManagedProfile();
+    public static boolean isManagedProfile(@NonNull UserManager userManager) {
+        return isManagedProfile(userManager, UserHandle.myUserId());
+    }
+
+    /**
+     * Returns true if the userId passed in is a managed profile.
+     *
+     * @throws IllegalArgumentException if userManager is null.
+     */
+    public static boolean isManagedProfile(@NonNull UserManager userManager, int userId) {
+        if (userManager == null) {
+            throw new IllegalArgumentException("userManager must not be null");
+        }
+        UserInfo userInfo = userManager.getUserInfo(userId);
+        return (userInfo != null) ? userInfo.isManagedProfile() : false;
     }
 
     /**
@@ -932,17 +960,17 @@
     }
 
     /**
-     * Returns the user id present in the bundle with {@link ChooseLockGeneric#KEY_USER_ID} if it
+     * Returns the user id present in the bundle with {@link Intent#EXTRA_USER_ID} if it
      * belongs to the current user.
      *
      * @throws SecurityException if the given userId does not belong to the current user group.
      */
-    public static int getSameOwnerUserId(Context context, Bundle bundle) {
+    public static int getUserIdFromBundle(Context context, Bundle bundle) {
         if (bundle == null) {
-            return getEffectiveUserId(context);
+            return getCredentialOwnerUserId(context);
         }
         int userId = bundle.getInt(Intent.EXTRA_USER_ID, UserHandle.myUserId());
-        return getSameOwnerUserId(context, userId);
+        return enforceSameOwner(context, userId);
     }
 
     /**
@@ -950,27 +978,28 @@
      *
      * @throws SecurityException if the given userId does not belong to the current user group.
      */
-    public static int getSameOwnerUserId(Context context, int userId) {
-        UserManager um = UserManager.get(context);
-        if (um != null) {
-            if (um.getUserProfiles().contains(new UserHandle(userId))) {
-                return userId;
-            } else {
-                throw new SecurityException("Given user id " + userId + " does not belong to user "
-                        + UserHandle.myUserId());
-            }
+    public static int enforceSameOwner(Context context, int userId) {
+        UserManager um = getUserManager(context);
+        if (!um.getUserProfiles().contains(new UserHandle(userId))) {
+            throw new SecurityException("Given user id " + userId + " does not belong to user "
+                    + UserHandle.myUserId());
         }
-        return getEffectiveUserId(context);
+        return userId;
     }
 
-    public static int getEffectiveUserId(Context context) {
-        UserManager um = UserManager.get(context);
-        if (um != null) {
-            return um.getCredentialOwnerProfile(UserHandle.myUserId());
-        } else {
-            Log.e(TAG, "Unable to acquire UserManager");
-            return UserHandle.myUserId();
-        }
+    /**
+     * Returns the effective credential owner of the calling user.
+     */
+    public static int getCredentialOwnerUserId(Context context) {
+        return getCredentialOwnerUserId(context, UserHandle.myUserId());
+    }
+
+    /**
+     * Returns the user id of the credential owner of the given user id.
+     */
+    public static int getCredentialOwnerUserId(Context context, int userId) {
+        UserManager um = getUserManager(context);
+        return um.getCredentialOwnerProfile(userId);
     }
 
     public static int resolveResource(Context context, int attr) {