Add padlock to location for work profile when disabled by admin.

Change-Id: Iee930c9340436cc68ee432ad15ec182ccc6fb0a1
diff --git a/res/values/strings.xml b/res/values/strings.xml
index e3c105f..eca018c 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -2827,8 +2827,6 @@
     <string name="location_title">My Location</string>
     <!-- [CHAR LIMIT=30] Title for managed profile location switch  -->
     <string name="managed_profile_location_switch_title">Location for work profile</string>
-    <!-- [CHAR LIMIT=30] Text to show on managed profile location switch if MDM has locked down location access for managed profile-->
-    <string name="managed_profile_location_switch_lockdown">Turned off by your company</string>
     <!-- [CHAR LIMIT=30] Location settings screen, setting preference screen box label for location mode -->
     <string name="location_mode_title">Mode</string>
     <!-- [CHAR LIMIT=30] Location settings screen, high accuracy location mode -->
diff --git a/res/xml/location_settings.xml b/res/xml/location_settings.xml
index 4ccc14a..d22d32a 100644
--- a/res/xml/location_settings.xml
+++ b/res/xml/location_settings.xml
@@ -27,10 +27,10 @@
             android:summary="@string/location_mode_location_off_title" />
 
         <!-- This preference category gets removed if there is no managed profile -->
-        <SwitchPreference
+        <com.android.settingslib.RestrictedSwitchPreference
             android:key="managed_profile_location_switch"
             android:title="@string/managed_profile_location_switch_title"
-            android:summary="@string/managed_profile_location_switch_lockdown"
+            settings:useAdminDisabledSummary="true"
             android:persistent="false"
             android:enabled="false"
             android:selectable="true" />
diff --git a/src/com/android/settings/location/LocationSettings.java b/src/com/android/settings/location/LocationSettings.java
index 047fcbb..c45de16 100644
--- a/src/com/android/settings/location/LocationSettings.java
+++ b/src/com/android/settings/location/LocationSettings.java
@@ -32,7 +32,6 @@
 import android.support.v7.preference.PreferenceCategory;
 import android.support.v7.preference.PreferenceGroup;
 import android.support.v7.preference.PreferenceScreen;
-import android.support.v14.preference.SwitchPreference;
 import android.util.Log;
 import android.view.Menu;
 import android.view.MenuInflater;
@@ -47,6 +46,7 @@
 import com.android.settings.dashboard.SummaryLoader;
 import com.android.settings.widget.SwitchBar;
 import com.android.settingslib.RestrictedLockUtils;
+import com.android.settingslib.RestrictedSwitchPreference;
 import com.android.settingslib.location.RecentLocationApps;
 
 import java.util.ArrayList;
@@ -104,7 +104,7 @@
     private Switch mSwitch;
     private boolean mValidListener = false;
     private UserHandle mManagedProfile;
-    private SwitchPreference mManagedProfileSwitch;
+    private RestrictedSwitchPreference mManagedProfileSwitch;
     private Preference mLocationMode;
     private PreferenceCategory mCategoryRecentLocationRequests;
     /** Receives UPDATE_INTENT  */
@@ -253,7 +253,7 @@
             root.removePreference(root.findPreference(KEY_MANAGED_PROFILE_SWITCH));
             mManagedProfileSwitch = null;
         } else {
-            mManagedProfileSwitch = (SwitchPreference)root
+            mManagedProfileSwitch = (RestrictedSwitchPreference)root
                     .findPreference(KEY_MANAGED_PROFILE_SWITCH);
             mManagedProfileSwitch.setOnPreferenceClickListener(null);
         }
@@ -263,26 +263,30 @@
         if (mManagedProfileSwitch == null) {
             return;
         }
-        boolean enabled = mainSwitchOn;
-        int summaryResId = R.string.switch_off_text;
-        if (mUm.hasUserRestriction(UserManager.DISALLOW_SHARE_LOCATION, mManagedProfile)
-                && getAdminRestrictingManagedProfile() != null) {
-                    summaryResId = R.string.managed_profile_location_switch_lockdown;
-                    enabled = false;
-        }
-
-        mManagedProfileSwitch.setEnabled(enabled);
         mManagedProfileSwitch.setOnPreferenceClickListener(null);
-        if (!enabled) {
+        final EnforcedAdmin admin = RestrictedLockUtils.checkIfRestrictionEnforced(getActivity(),
+                UserManager.DISALLOW_SHARE_LOCATION, mManagedProfile.getIdentifier());
+        if (mUm.hasUserRestriction(UserManager.DISALLOW_SHARE_LOCATION, mManagedProfile)
+                && admin != null) {
+            mManagedProfileSwitch.setDisabledByAdmin(admin);
             mManagedProfileSwitch.setChecked(false);
         } else {
-            final boolean isRestricted = isManagedProfileRestrictedByBase();
-            mManagedProfileSwitch.setChecked(!isRestricted);
-            summaryResId = (isRestricted ?
-                    R.string.switch_off_text : R.string.switch_on_text);
-            mManagedProfileSwitch.setOnPreferenceClickListener(mManagedProfileSwitchClickListener);
+            boolean enabled = mainSwitchOn;
+            mManagedProfileSwitch.setEnabled(enabled);
+
+            int summaryResId = R.string.switch_off_text;
+            if (!enabled) {
+                mManagedProfileSwitch.setChecked(false);
+            } else {
+                final boolean isRestricted = isManagedProfileRestrictedByBase();
+                mManagedProfileSwitch.setChecked(!isRestricted);
+                summaryResId = (isRestricted ?
+                        R.string.switch_off_text : R.string.switch_on_text);
+                mManagedProfileSwitch.setOnPreferenceClickListener(
+                        mManagedProfileSwitchClickListener);
+            }
+            mManagedProfileSwitch.setSummary(summaryResId);
         }
-        mManagedProfileSwitch.setSummary(summaryResId);
     }
 
     /**
@@ -418,27 +422,6 @@
         }
     }
 
-    private ComponentName getAdminRestrictingManagedProfile() {
-        if (mManagedProfile == null) {
-            return null;
-        }
-        DevicePolicyManager dpm = (DevicePolicyManager)getActivity().getSystemService(
-                Context.DEVICE_POLICY_SERVICE);
-        if (dpm == null) {
-            return null;
-        }
-        List<ComponentName> admins = dpm.getActiveAdminsAsUser(mManagedProfile.getIdentifier());
-        for (int i = 0; i < admins.size(); ++i) {
-            final ComponentName admin = admins.get(i);
-            Bundle restrictions = dpm.getUserRestrictions(admin, mManagedProfile.getIdentifier());
-            if (restrictions != null && restrictions.getBoolean(UserManager.DISALLOW_SHARE_LOCATION,
-                    false)) {
-                return admin;
-            }
-        }
-        return null;
-    }
-
     private boolean isManagedProfileRestrictedByBase() {
         if (mManagedProfile == null) {
             return false;