Updated RestrictedSwitchPreference

Updated RestrictedSwitchPreference with new policy transparency changes.

Bug: 26763080
Change-Id: I187e38b157cc800626e1fbaffc9a2962a21be3c3
diff --git a/Android.mk b/Android.mk
index 002dee5..f3fff79 100644
--- a/Android.mk
+++ b/Android.mk
@@ -32,6 +32,8 @@
 
 LOCAL_PROGUARD_FLAG_FILES := proguard.flags sip/proguard.flags
 
+include frameworks/base/packages/SettingsLib/common.mk
+
 include $(BUILD_PACKAGE)
 
 # Build the test package
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index 3cc535a..dddbb32 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -131,6 +131,12 @@
     <uses-permission android:name="android.permission.CHANGE_DEVICE_IDLE_TEMP_WHITELIST" />
     <uses-permission android:name="android.permission.READ_BLOCKED_NUMBERS" />
 
+    <!-- Needed for passing extras with intent ACTION_SHOW_ADMIN_SUPPORT_DETAILS -->
+    <uses-permission android:name="android.permission.MANAGE_DEVICE_ADMINS" />
+
+    <!-- Needed for calling DPM.getUserRestrictions() -->
+    <uses-permission android:name="android.permission.MANAGE_PROFILE_AND_DEVICE_OWNERS" />
+
     <!-- This tells the activity manager to not delay any of our activity
          start requests, even if they happen immediately after the user
          presses home. -->
diff --git a/res/drawable/ic_settings_lock_outline.xml b/res/drawable/ic_settings_lock_outline.xml
deleted file mode 100644
index fdc67f5..0000000
--- a/res/drawable/ic_settings_lock_outline.xml
+++ /dev/null
@@ -1,27 +0,0 @@
-<?xml version="1.0" encoding="utf-8"?>
-<!--
-     Copyright (C) 2015 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.
--->
-
-<vector xmlns:android="http://schemas.android.com/apk/res/android"
-        android:width="24dp"
-        android:height="24dp"
-        android:viewportWidth="24.0"
-        android:viewportHeight="24.0"
-        android:tint="?android:attr/colorAccent">
-    <path
-            android:fillColor="@android:color/white"
-            android:pathData="M12 17c1.1 0 2-.9 2-2s-.9-2-2-2-2 .9-2 2 .9 2 2 2zm6-9h-1V6c0-2.76-2.24-5-5-5S7 3.24 7 6v2H6c-1.1 0-2 .9-2 2v10c0 1.1.9 2 2 2h12c1.1 0 2-.9 2-2V10c0-1.1-.9-2-2-2zM8.9 6c0-1.71 1.39-3.1 3.1-3.1s3.1 1.39 3.1 3.1v2H8.9V6zM18 20H6V10h12v10z" />
-</vector>
diff --git a/res/values/dimens.xml b/res/values/dimens.xml
index 886d829..a3c62a9 100644
--- a/res/values/dimens.xml
+++ b/res/values/dimens.xml
@@ -107,8 +107,4 @@
     <dimen name="floating_action_button_width">67dp</dimen>
     <dimen name="floating_action_button_height">67dp</dimen>
     <dimen name="floating_action_button_margin_bottom">26dp</dimen>
-
-    <!-- Lock icon for preferences locked by admin -->
-    <dimen name="restricted_lock_icon_size">16dp</dimen>
-    <dimen name="restricted_lock_icon_padding">4dp</dimen>
 </resources>
diff --git a/src/com/android/phone/RestrictedSwitchPreference.java b/src/com/android/phone/RestrictedSwitchPreference.java
index 6af75b7..1fdf22f 100644
--- a/src/com/android/phone/RestrictedSwitchPreference.java
+++ b/src/com/android/phone/RestrictedSwitchPreference.java
@@ -17,33 +17,25 @@
 package com.android.phone;
 
 import android.content.Context;
-import android.content.Intent;
-import android.graphics.drawable.Drawable;
 import android.os.UserManager;
 import android.preference.PreferenceScreen;
 import android.preference.SwitchPreference;
-import android.provider.Settings;
 import android.util.AttributeSet;
 import android.view.View;
 import android.widget.TextView;
 
+import com.android.settingslib.RestrictedLockUtils;
+import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
+
 public class RestrictedSwitchPreference extends SwitchPreference {
     private final Context mContext;
-    private final Drawable mRestrictedPadlock;
-    private final int mRestrictedPadlockPadding;
     private boolean mDisabledByAdmin;
+    private EnforcedAdmin mEnforcedAdmin;
 
-    public RestrictedSwitchPreference(Context context, AttributeSet attrs,
-            int defStyleAttr, int defStyleRes) {
+    public RestrictedSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr,
+            int defStyleRes) {
         super(context, attrs, defStyleAttr, defStyleRes);
         mContext = context;
-        mRestrictedPadlock = mContext
-                .getDrawable(R.drawable.ic_settings_lock_outline);
-        final int iconSize = mContext.getResources().getDimensionPixelSize(
-                R.dimen.restricted_lock_icon_size);
-        mRestrictedPadlock.setBounds(0, 0, iconSize, iconSize);
-        mRestrictedPadlockPadding = mContext.getResources()
-                .getDimensionPixelSize(R.dimen.restricted_lock_icon_padding);
     }
 
     public RestrictedSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr) {
@@ -63,31 +55,40 @@
         super.onBindView(view);
         final TextView titleView = (TextView) view.findViewById(com.android.internal.R.id.title);
         if (titleView != null) {
+            RestrictedLockUtils.setTextViewPadlock(mContext, titleView, mDisabledByAdmin);
             if (mDisabledByAdmin) {
                 view.setEnabled(true);
-                titleView.setCompoundDrawablesRelative(null, null, mRestrictedPadlock, null);
-                titleView.setCompoundDrawablePadding(mRestrictedPadlockPadding);
-            } else {
-                titleView.setCompoundDrawablesRelative(null, null, null, null);
             }
         }
     }
 
     public void checkRestrictionAndSetDisabled(String userRestriction) {
-        final UserManager mUm = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
-        if (mUm.hasUserRestriction(userRestriction)) {
-            mDisabledByAdmin = true;
-            setEnabled(false);
+        setDisabledByAdmin(RestrictedLockUtils.checkIfRestrictionEnforced(mContext, userRestriction,
+                UserManager.get(mContext).getUserHandle()));
+    }
+
+    @Override
+    public void setEnabled(boolean enabled) {
+        if (enabled && mDisabledByAdmin) {
+            setDisabledByAdmin(null);
         } else {
-            mDisabledByAdmin = false;
+            super.setEnabled(enabled);
+        }
+    }
+
+    public void setDisabledByAdmin(EnforcedAdmin admin) {
+        final boolean disabled = (admin != null ? true : false);
+        mEnforcedAdmin = admin;
+        if (mDisabledByAdmin != disabled) {
+            mDisabledByAdmin = disabled;
+            setEnabled(!disabled);
         }
     }
 
     @Override
     public void performClick(PreferenceScreen preferenceScreen) {
         if (mDisabledByAdmin) {
-            Intent intent = new Intent(Settings.ACTION_SHOW_ADMIN_SUPPORT_DETAILS);
-            mContext.startActivity(intent);
+            RestrictedLockUtils.sendShowAdminSupportDetailsIntent(mContext, mEnforcedAdmin);
         } else {
             super.performClick(preferenceScreen);
         }