Add disabled by policy message as empty views in preferencefragment.

Change-Id: I296dc02c8e5cbea74f8415f4c8c5723f85e20b5b
diff --git a/src/com/android/settings/RestrictedSettingsFragment.java b/src/com/android/settings/RestrictedSettingsFragment.java
index c78d6e2..3642fa6 100644
--- a/src/com/android/settings/RestrictedSettingsFragment.java
+++ b/src/com/android/settings/RestrictedSettingsFragment.java
@@ -17,6 +17,7 @@
 package com.android.settings;
 
 import android.app.Activity;
+import android.app.admin.DevicePolicyManager;
 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
@@ -24,10 +25,18 @@
 import android.content.RestrictionsManager;
 import android.os.Bundle;
 import android.os.PersistableBundle;
+import android.os.UserHandle;
 import android.os.UserManager;
+import android.view.View;
+import android.widget.TextView;
+
+import com.android.settingslib.RestrictedLockUtils;
+
+import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin;
 
 /**
- * Base class for settings screens that should be pin protected when in restricted mode.
+ * Base class for settings screens that should be pin protected when in restricted mode or
+ * that will display an admin support message in case an admin has disabled the options.
  * The constructor for this class will take the restriction key that this screen should be
  * locked by.  If {@link RestrictionsManager.hasRestrictionsProvider()} and
  * {@link UserManager.hasUserRestriction()}, then the user will have to enter the restrictions
@@ -37,7 +46,8 @@
  * {@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 {
+public abstract class RestrictedSettingsFragment extends SettingsPreferenceFragment
+            implements View.OnClickListener {
 
     protected static final String RESTRICT_IF_OVERRIDABLE = "restrict_if_overridable";
 
@@ -55,6 +65,9 @@
     private RestrictionsManager mRestrictionsManager;
 
     private final String mRestrictionKey;
+    private View mAdminSupportDetails;
+    private EnforcedAdmin mEnforcedAdmin;
+    private TextView mEmptyTextView;
 
     // Receiver to clear pin status when the screen is turned off.
     private BroadcastReceiver mScreenOffReceiver = new BroadcastReceiver() {
@@ -95,6 +108,13 @@
     }
 
     @Override
+    public void onActivityCreated(Bundle savedInstanceState) {
+        super.onActivityCreated(savedInstanceState);
+        mAdminSupportDetails = initAdminSupportDetailsView();
+        mEmptyTextView = initEmptyTextView();
+    }
+
+    @Override
     public void onSaveInstanceState(Bundle outState) {
         super.onSaveInstanceState(outState);
 
@@ -178,6 +198,68 @@
         return restricted && mRestrictionsManager.hasRestrictionsProvider();
     }
 
+    protected View initAdminSupportDetailsView() {
+        return null;
+    }
+
+    protected TextView initEmptyTextView() {
+        return null;
+    }
+
+    private void updateAdminSupportDetailsView() {
+        mEnforcedAdmin = RestrictedLockUtils.checkIfRestrictionEnforced(getActivity(),
+                mRestrictionKey, UserHandle.myUserId());
+        if (mEnforcedAdmin != null) {
+            final Activity activity = getActivity();
+            DevicePolicyManager dpm = (DevicePolicyManager) activity.getSystemService(
+                    Context.DEVICE_POLICY_SERVICE);
+            if (mEnforcedAdmin.userId == UserHandle.USER_NULL) {
+                mEnforcedAdmin.userId = UserHandle.myUserId();
+            }
+            CharSequence supportMessage = dpm.getShortSupportMessageForUser(
+                    mEnforcedAdmin.component, mEnforcedAdmin.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);
+        }
+    }
+
+    @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;
+    }
+
+    @Override
+    protected void onDataSetChanged() {
+        highlightPreferenceIfNeeded();
+        if (mAdminSupportDetails != null && isUiRestricted()) {
+            updateAdminSupportDetailsView();
+            setEmptyView(mAdminSupportDetails);
+        } else if (mEmptyTextView != null) {
+            setEmptyView(mEmptyTextView);
+        }
+        super.onDataSetChanged();
+    }
+
     /**
      * Returns whether restricted or actionable UI elements should be removed or disabled.
      */
diff --git a/src/com/android/settings/SettingsPreferenceFragment.java b/src/com/android/settings/SettingsPreferenceFragment.java
index 4f81a38..d8b9b91 100644
--- a/src/com/android/settings/SettingsPreferenceFragment.java
+++ b/src/com/android/settings/SettingsPreferenceFragment.java
@@ -176,13 +176,6 @@
         unregisterObserverIfNeeded();
     }
 
-    @Override
-    public void onStop() {
-        super.onStop();
-
-        unregisterObserverIfNeeded();
-    }
-
     public void showLoadingWhenEmpty() {
         View loading = getView().findViewById(R.id.loading_container);
         setEmptyView(loading);
@@ -220,7 +213,7 @@
         }
     }
 
-    private void onDataSetChanged() {
+    protected void onDataSetChanged() {
         highlightPreferenceIfNeeded();
         updateEmptyView();
     }
@@ -290,6 +283,9 @@
     }
 
     public void setEmptyView(View v) {
+        if (mEmptyView != null) {
+            mEmptyView.setVisibility(View.GONE);
+        }
         mEmptyView = v;
         updateEmptyView();
     }
diff --git a/src/com/android/settings/bluetooth/BluetoothSettings.java b/src/com/android/settings/bluetooth/BluetoothSettings.java
index eac89b1..f71dd0a 100644
--- a/src/com/android/settings/bluetooth/BluetoothSettings.java
+++ b/src/com/android/settings/bluetooth/BluetoothSettings.java
@@ -87,7 +87,6 @@
     private boolean mInitialScanStarted;
     private boolean mInitiateDiscoverable;
 
-    private TextView mEmptyView;
     private SwitchBar mSwitchBar;
 
     private final IntentFilter mIntentFilter;
@@ -136,10 +135,6 @@
         mInitialScanStarted = false;
         mInitiateDiscoverable = true;
 
-        mEmptyView = (TextView) getView().findViewById(android.R.id.empty);
-        setEmptyView(mEmptyView);
-        mEmptyView.setGravity(Gravity.START | Gravity.CENTER_VERTICAL);
-
         final SettingsActivity activity = (SettingsActivity) getActivity();
         mSwitchBar = activity.getSwitchBar();
 
@@ -175,7 +170,6 @@
         if (isUiRestricted()) {
             setDeviceListGroup(getPreferenceScreen());
             removeAllDevices();
-            mEmptyView.setText(R.string.bluetooth_empty_list_user_restricted);
             return;
         }
 
@@ -298,7 +292,6 @@
                 mDevicePreferenceMap.clear();
 
                 if (isUiRestricted()) {
-                    messageId = R.string.bluetooth_empty_list_user_restricted;
                     break;
                 }
 
@@ -360,7 +353,7 @@
             case BluetoothAdapter.STATE_OFF:
                 setOffMessage();
                 if (isUiRestricted()) {
-                    messageId = R.string.bluetooth_empty_list_user_restricted;
+                    messageId = 0;
                 }
                 break;
 
@@ -373,15 +366,28 @@
         setDeviceListGroup(preferenceScreen);
         removeAllDevices();
         if (messageId != 0) {
-            mEmptyView.setText(messageId);
+            getEmptyTextView().setText(messageId);
         }
         if (!isUiRestricted()) {
             getActivity().invalidateOptionsMenu();
         }
     }
 
+    @Override
+    protected TextView initEmptyTextView() {
+        TextView textView = (TextView) getView().findViewById(android.R.id.empty);
+        textView.setGravity(Gravity.START | Gravity.CENTER_VERTICAL);
+        return textView;
+    }
+
+    @Override
+    protected View initAdminSupportDetailsView() {
+        return getActivity().findViewById(R.id.admin_support_details);
+    }
+
     private void setOffMessage() {
-        if (mEmptyView == null) {
+        final TextView emptyView = getEmptyTextView();
+        if (emptyView == null) {
             return;
         }
         final CharSequence briefText = getText(R.string.bluetooth_empty_list_bluetooth_off);
@@ -392,13 +398,13 @@
 
         if (!bleScanningMode) {
             // Show only the brief text if the scanning mode has been turned off.
-            mEmptyView.setText(briefText, TextView.BufferType.SPANNABLE);
+            emptyView.setText(briefText, TextView.BufferType.SPANNABLE);
         } else {
             final StringBuilder contentBuilder = new StringBuilder();
             contentBuilder.append(briefText);
             contentBuilder.append("\n\n");
             contentBuilder.append(getText(R.string.ble_scan_notify_text));
-            LinkifyUtils.linkify(mEmptyView, contentBuilder, new LinkifyUtils.OnClickListener() {
+            LinkifyUtils.linkify(emptyView, contentBuilder, new LinkifyUtils.OnClickListener() {
                 @Override
                 public void onClick() {
                     final SettingsActivity activity =
@@ -409,7 +415,7 @@
             });
         }
         getPreferenceScreen().removeAll();
-        Spannable boldSpan = (Spannable) mEmptyView.getText();
+        Spannable boldSpan = (Spannable) emptyView.getText();
         boldSpan.setSpan(
                 new TextAppearanceSpan(getActivity(), android.R.style.TextAppearance_Medium), 0,
                 briefText.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
diff --git a/src/com/android/settings/wifi/WifiSettings.java b/src/com/android/settings/wifi/WifiSettings.java
index 403d12a..8be0e79 100644
--- a/src/com/android/settings/wifi/WifiSettings.java
+++ b/src/com/android/settings/wifi/WifiSettings.java
@@ -131,7 +131,6 @@
     private WifiDialog mDialog;
     private WriteWifiConfigToNfcDialog mWifiToNfcDialog;
 
-    private TextView mEmptyView;
     private ProgressBar mProgressHeader;
 
     // this boolean extra specifies whether to disable the Next button when not connected. Used by
@@ -280,7 +279,6 @@
             }
         }
 
-        mEmptyView = initEmptyView();
         registerForContextMenu(getListView());
         setHasOptionsMenu(true);
 
@@ -628,9 +626,8 @@
     public void onAccessPointsChanged() {
         // Safeguard from some delayed event handling
         if (getActivity() == null) return;
-
         if (isUiRestricted()) {
-            addMessagePreference(R.string.wifi_empty_list_user_restricted);
+            getPreferenceScreen().removeAll();
             return;
         }
         final int wifiState = mWifiManager.getWifiState();
@@ -708,15 +705,26 @@
         }
     }
 
-    protected TextView initEmptyView() {
+    @Override
+    protected TextView initEmptyTextView() {
         TextView emptyView = (TextView) getActivity().findViewById(android.R.id.empty);
         emptyView.setGravity(Gravity.START | Gravity.CENTER_VERTICAL);
-        setEmptyView(emptyView);
         return emptyView;
     }
 
+    @Override
+    protected View initAdminSupportDetailsView() {
+        return getActivity().findViewById(R.id.admin_support_details);
+    }
+
     private void setOffMessage() {
-        if (mEmptyView == null) {
+        if (isUiRestricted()) {
+            getPreferenceScreen().removeAll();
+            return;
+        }
+
+        TextView emptyTextView = getEmptyTextView();
+        if (emptyTextView == null) {
             return;
         }
 
@@ -729,17 +737,17 @@
         final boolean wifiScanningMode = Settings.Global.getInt(
                 resolver, Settings.Global.WIFI_SCAN_ALWAYS_AVAILABLE, 0) == 1;
 
-        if (isUiRestricted() || !wifiScanningMode) {
+        if (!wifiScanningMode) {
             // Show only the brief text if the user is not allowed to configure scanning settings,
             // or the scanning mode has been turned off.
-            mEmptyView.setText(briefText, BufferType.SPANNABLE);
+            emptyTextView.setText(briefText, BufferType.SPANNABLE);
         } else {
             // Append the description of scanning settings with link.
             final StringBuilder contentBuilder = new StringBuilder();
             contentBuilder.append(briefText);
             contentBuilder.append("\n\n");
             contentBuilder.append(getText(R.string.wifi_scan_notify_text));
-            LinkifyUtils.linkify(mEmptyView, contentBuilder, new LinkifyUtils.OnClickListener() {
+            LinkifyUtils.linkify(emptyTextView, contentBuilder, new LinkifyUtils.OnClickListener() {
                 @Override
                 public void onClick() {
                     final SettingsActivity activity =
@@ -750,7 +758,7 @@
             });
         }
         // Embolden and enlarge the brief description anyway.
-        Spannable boldSpan = (Spannable) mEmptyView.getText();
+        Spannable boldSpan = (Spannable) emptyTextView.getText();
         boldSpan.setSpan(
                 new TextAppearanceSpan(getActivity(), android.R.style.TextAppearance_Medium), 0,
                 briefText.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
@@ -758,7 +766,8 @@
     }
 
     private void addMessagePreference(int messageId) {
-        if (mEmptyView != null) mEmptyView.setText(messageId);
+        TextView emptyTextView = getEmptyTextView();
+        if (emptyTextView != null) emptyTextView.setText(messageId);
         getPreferenceScreen().removeAll();
     }
 
diff --git a/src/com/android/settings/wifi/WifiSettingsForSetupWizard.java b/src/com/android/settings/wifi/WifiSettingsForSetupWizard.java
index 096847d..2f35478 100644
--- a/src/com/android/settings/wifi/WifiSettingsForSetupWizard.java
+++ b/src/com/android/settings/wifi/WifiSettingsForSetupWizard.java
@@ -144,7 +144,7 @@
     }
 
     @Override
-    protected TextView initEmptyView() {
+    protected TextView initEmptyTextView() {
         final LayoutInflater inflater = LayoutInflater.from(getActivity());
         mEmptyFooter = (TextView) inflater.inflate(R.layout.setup_wifi_empty, getListView(), false);
         return mEmptyFooter;