Merge "Avoid triggering device admin settings in non-current user." into nyc-dev
diff --git a/src/com/android/settings/RestrictedSettingsFragment.java b/src/com/android/settings/RestrictedSettingsFragment.java
index 711ef8f..ce8a4ad 100644
--- a/src/com/android/settings/RestrictedSettingsFragment.java
+++ b/src/com/android/settings/RestrictedSettingsFragment.java
@@ -47,8 +47,7 @@
  * {@link RestrictionsManager.hasRestrictionsProvider()} returns true, pass in
  * {@link RESTRICT_IF_OVERRIDABLE} to the constructor instead of a restrictions key.
  */
-public abstract class RestrictedSettingsFragment extends SettingsPreferenceFragment
-            implements View.OnClickListener {
+public abstract class RestrictedSettingsFragment extends SettingsPreferenceFragment {
 
     protected static final String RESTRICT_IF_OVERRIDABLE = "restrict_if_overridable";
 
@@ -212,22 +211,6 @@
         return emptyView;
     }
 
-    private void updateAdminSupportDetailsView() {
-        final EnforcedAdmin admin = getRestrictionEnforcedAdmin();
-        if (admin != null) {
-            final Activity activity = getActivity();
-            DevicePolicyManager dpm = (DevicePolicyManager) activity.getSystemService(
-                    Context.DEVICE_POLICY_SERVICE);
-            CharSequence supportMessage = dpm.getShortSupportMessageForUser(
-                    admin.component, admin.userId);
-            if (supportMessage != null) {
-                TextView textView = (TextView) activity.findViewById(R.id.admin_support_msg);
-                textView.setText(supportMessage);
-            }
-            activity.findViewById(R.id.admins_policies_list).setOnClickListener(this);
-        }
-    }
-
     public EnforcedAdmin getRestrictionEnforcedAdmin() {
         mEnforcedAdmin = RestrictedLockUtils.checkIfRestrictionEnforced(getActivity(),
                 mRestrictionKey, UserHandle.myUserId());
@@ -237,24 +220,6 @@
         return mEnforcedAdmin;
     }
 
-    @Override
-    public void onClick(View view) {
-        Intent intent = new Intent();
-        if (view.getId() == R.id.admins_policies_list && mEnforcedAdmin != null) {
-            if (mEnforcedAdmin.component != null) {
-                intent.setClass(getActivity(), DeviceAdminAdd.class);
-                intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mEnforcedAdmin.component);
-                // DeviceAdminAdd class may need to run as managed profile.
-                getActivity().startActivityAsUser(intent, UserHandle.of(mEnforcedAdmin.userId));
-            } else {
-                intent.setClass(getActivity(), Settings.DeviceAdminSettingsActivity.class);
-                // Activity merges both managed profile and parent users
-                // admins so show as same user as this activity.
-                getActivity().startActivity(intent);
-            }
-        }
-    }
-
     public TextView getEmptyTextView() {
         return mEmptyTextView;
     }
diff --git a/src/com/android/settings/ShowAdminSupportDetailsDialog.java b/src/com/android/settings/ShowAdminSupportDetailsDialog.java
index 3b679eb..152a89c 100644
--- a/src/com/android/settings/ShowAdminSupportDetailsDialog.java
+++ b/src/com/android/settings/ShowAdminSupportDetailsDialog.java
@@ -39,7 +39,8 @@
 import android.widget.ImageView;
 import android.widget.TextView;
 
-import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
+import com.android.settingslib.RestrictedLockUtils;
+import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
 
 public class ShowAdminSupportDetailsDialog extends Activity
         implements DialogInterface.OnDismissListener {
@@ -104,20 +105,26 @@
         return false;
     }
 
-    private void initializeDialogViews(View root, final ComponentName admin, int userId) {
+    private void initializeDialogViews(View root, ComponentName admin, int userId) {
         if (admin != null) {
-            ActivityInfo ai = null;
-            try {
-                ai = AppGlobals.getPackageManager().getReceiverInfo(admin, 0 /* flags */, userId);
-            } catch (RemoteException e) {
-                Log.w(TAG, "Missing reciever info" , e);
-            }
-            if (ai != null) {
-                Drawable icon = ai.loadIcon(getPackageManager());
-                Drawable badgedIcon = getPackageManager().getUserBadgedIcon(
-                        icon, new UserHandle(userId));
-                ((ImageView) root.findViewById(R.id.admin_support_icon)).setImageDrawable(
-                        badgedIcon);
+            if (!RestrictedLockUtils.isAdminInCurrentUserOrProfile(this, admin)
+                    || !RestrictedLockUtils.isCurrentUserOrProfile(this, userId)) {
+                admin = null;
+            } else {
+                ActivityInfo ai = null;
+                try {
+                    ai = AppGlobals.getPackageManager().getReceiverInfo(admin, 0 /* flags */,
+                            userId);
+                } catch (RemoteException e) {
+                    Log.w(TAG, "Missing reciever info", e);
+                }
+                if (ai != null) {
+                    Drawable icon = ai.loadIcon(getPackageManager());
+                    Drawable badgedIcon = getPackageManager().getUserBadgedIcon(
+                            icon, new UserHandle(userId));
+                    ((ImageView) root.findViewById(R.id.admin_support_icon)).setImageDrawable(
+                            badgedIcon);
+                }
             }
         }
 
@@ -129,20 +136,27 @@
         if (enforcedAdmin == null) {
             return;
         }
+
         if (enforcedAdmin.component != null) {
             DevicePolicyManager dpm = (DevicePolicyManager) activity.getSystemService(
                     Context.DEVICE_POLICY_SERVICE);
-            if (enforcedAdmin.userId == UserHandle.USER_NULL) {
-                enforcedAdmin.userId = UserHandle.myUserId();
-            }
-            CharSequence supportMessage = null;
-            if (UserHandle.isSameApp(Process.myUid(), Process.SYSTEM_UID)) {
-                supportMessage = dpm.getShortSupportMessageForUser(
-                        enforcedAdmin.component, enforcedAdmin.userId);
-            }
-            if (supportMessage != null) {
-                TextView textView = (TextView) root.findViewById(R.id.admin_support_msg);
-                textView.setText(supportMessage);
+            if (!RestrictedLockUtils.isAdminInCurrentUserOrProfile(activity,
+                    enforcedAdmin.component) || !RestrictedLockUtils.isCurrentUserOrProfile(
+                    activity, enforcedAdmin.userId)) {
+                enforcedAdmin.component = null;
+            } else {
+                if (enforcedAdmin.userId == UserHandle.USER_NULL) {
+                    enforcedAdmin.userId = UserHandle.myUserId();
+                }
+                CharSequence supportMessage = null;
+                if (UserHandle.isSameApp(Process.myUid(), Process.SYSTEM_UID)) {
+                    supportMessage = dpm.getShortSupportMessageForUser(
+                            enforcedAdmin.component, enforcedAdmin.userId);
+                }
+                if (supportMessage != null) {
+                    TextView textView = (TextView) root.findViewById(R.id.admin_support_msg);
+                    textView.setText(supportMessage);
+                }
             }
         }