Add DISALLOW_DATA_ROAMING restriction support to settings

Added RestrictedSwitchPreference to generalize device admin restriction
support for Telephony. Implemented handling of DISALLOW_DATA_ROAMING
restriction using this.

Bug: 24890464
Change-Id: Ice1fb169527c7612be732719523252f773283961

https://screenshot.googleplex.com/u76rW3HK4Jr
https://screenshot.googleplex.com/xqKUwVofyFB
diff --git a/src/com/android/phone/MobileNetworkSettings.java b/src/com/android/phone/MobileNetworkSettings.java
index 2522611..68c6047 100644
--- a/src/com/android/phone/MobileNetworkSettings.java
+++ b/src/com/android/phone/MobileNetworkSettings.java
@@ -35,7 +35,6 @@
 import android.content.Intent;
 import android.content.IntentFilter;
 import android.content.pm.PackageManager.NameNotFoundException;
-import android.content.pm.UserInfo;
 import android.net.Uri;
 import android.os.AsyncResult;
 import android.os.Bundle;
@@ -43,7 +42,6 @@
 import android.os.Message;
 import android.os.PersistableBundle;
 import android.os.SystemProperties;
-import android.os.UserHandle;
 import android.os.UserManager;
 import android.preference.ListPreference;
 import android.preference.Preference;
@@ -111,7 +109,7 @@
     //UI objects
     private ListPreference mButtonPreferredNetworkMode;
     private ListPreference mButtonEnabledNetworks;
-    private SwitchPreference mButtonDataRoam;
+    private RestrictedSwitchPreference mButtonDataRoam;
     private SwitchPreference mButton4glte;
     private Preference mLteDataServicePref;
 
@@ -449,7 +447,7 @@
         //get UI object references
         PreferenceScreen prefSet = getPreferenceScreen();
 
-        mButtonDataRoam = (SwitchPreference) prefSet.findPreference(BUTTON_ROAMING_KEY);
+        mButtonDataRoam = (RestrictedSwitchPreference) prefSet.findPreference(BUTTON_ROAMING_KEY);
         mButtonPreferredNetworkMode = (ListPreference) prefSet.findPreference(
                 BUTTON_PREFERED_NETWORK_MODE);
         mButtonEnabledNetworks = (ListPreference) prefSet.findPreference(
@@ -743,6 +741,9 @@
                 ImsManager.isNonTtyOrTtyOnVolteEnabled(getApplicationContext()) &&
                 carrierConfig.getBoolean(CarrierConfigManager.KEY_EDITABLE_ENHANCED_4G_LTE_BOOL);
         mButtonDataRoam.setEnabled(hasActiveSubscriptions);
+        if (mButtonDataRoam.isEnabled()) {
+            mButtonDataRoam.checkRestrictionAndSetDisabled(UserManager.DISALLOW_DATA_ROAMING);
+        }
         mButtonPreferredNetworkMode.setEnabled(hasActiveSubscriptions);
         mButtonEnabledNetworks.setEnabled(hasActiveSubscriptions);
         mButton4glte.setEnabled(hasActiveSubscriptions && canChange4glte);
diff --git a/src/com/android/phone/RestrictedSwitchPreference.java b/src/com/android/phone/RestrictedSwitchPreference.java
new file mode 100644
index 0000000..6af75b7
--- /dev/null
+++ b/src/com/android/phone/RestrictedSwitchPreference.java
@@ -0,0 +1,95 @@
+/*
+ * 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.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;
+
+public class RestrictedSwitchPreference extends SwitchPreference {
+    private final Context mContext;
+    private final Drawable mRestrictedPadlock;
+    private final int mRestrictedPadlockPadding;
+    private boolean mDisabledByAdmin;
+
+    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) {
+        this(context, attrs, defStyleAttr, 0);
+    }
+
+    public RestrictedSwitchPreference(Context context, AttributeSet attrs) {
+        this(context, attrs, com.android.internal.R.attr.switchPreferenceStyle);
+    }
+
+    public RestrictedSwitchPreference(Context context) {
+        this(context, null);
+    }
+
+    @Override
+    public void onBindView(View view) {
+        super.onBindView(view);
+        final TextView titleView = (TextView) view.findViewById(com.android.internal.R.id.title);
+        if (titleView != null) {
+            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);
+        } else {
+            mDisabledByAdmin = false;
+        }
+    }
+
+    @Override
+    public void performClick(PreferenceScreen preferenceScreen) {
+        if (mDisabledByAdmin) {
+            Intent intent = new Intent(Settings.ACTION_SHOW_ADMIN_SUPPORT_DETAILS);
+            mContext.startActivity(intent);
+        } else {
+            super.performClick(preferenceScreen);
+        }
+    }
+}