Use DimmableIconPreference for Add user action

Moved DimmableIconPreference from location to the root package since it is
now used in several places.

Add user action now uses DimmableIconPreference. Added a new summary string,
which is displayed when no more users can be added.

Bug: 20892920
Change-Id: I00b00f80ba8933a00a2de85777b9f7e55d03c31b
diff --git a/res/values/strings.xml b/res/values/strings.xml
index c00d904..63278a8 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -5425,6 +5425,8 @@
     <string name="user_nickname">Nickname</string>
     <!-- Title for add user type dialog [CHAR LIMIT=45] -->
     <string name="user_add_user_type_title">Add</string>
+    <!-- Summary for add user action, when it's disabled [CHAR LIMIT=100] -->
+    <string name="user_add_max_count">You can add up to <xliff:g id="user_count">%1$d</xliff:g> users</string>
     <!-- Summary for add user entry in the choice dialog [CHAR LIMIT=none] -->
     <string name="user_add_user_item_summary">Users have their own apps and content</string>
     <!-- Summary for add restricted profile entry in the choice dialog [CHAR LIMIT=none] -->
diff --git a/res/xml/user_settings.xml b/res/xml/user_settings.xml
index 6bbeffa..f33177c 100644
--- a/res/xml/user_settings.xml
+++ b/res/xml/user_settings.xml
@@ -23,7 +23,7 @@
             android:title="@string/user_list_title">
     </PreferenceCategory>
 
-    <Preference
+    <com.android.settings.DimmableIconPreference
             android:key="user_add"
             android:title="@string/user_add_user_or_profile_menu"
             android:icon="@drawable/ic_menu_add_dark" />
diff --git a/src/com/android/settings/location/DimmableIconPreference.java b/src/com/android/settings/DimmableIconPreference.java
similarity index 85%
rename from src/com/android/settings/location/DimmableIconPreference.java
rename to src/com/android/settings/DimmableIconPreference.java
index f785992..2d73603 100644
--- a/src/com/android/settings/location/DimmableIconPreference.java
+++ b/src/com/android/settings/DimmableIconPreference.java
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-package com.android.settings.location;
+package com.android.settings;
 
 import android.annotation.Nullable;
 import android.content.Context;
@@ -35,16 +35,9 @@
 
     private final CharSequence mContentDescription;
 
-    public DimmableIconPreference(Context context, AttributeSet attrs, int defStyle,
-            @Nullable CharSequence contentDescription) {
-        super(context, attrs, defStyle);
-        mContentDescription = contentDescription;
-    }
-
-    public DimmableIconPreference(Context context, AttributeSet attrs,
-            @Nullable CharSequence contentDescription) {
+    public DimmableIconPreference(Context context, AttributeSet attrs) {
         super(context, attrs);
-        mContentDescription = contentDescription;
+        mContentDescription = null;
     }
 
     public DimmableIconPreference(Context context, @Nullable CharSequence contentDescription) {
diff --git a/src/com/android/settings/location/RecentLocationApps.java b/src/com/android/settings/location/RecentLocationApps.java
index d7b62e7..66bdda5 100644
--- a/src/com/android/settings/location/RecentLocationApps.java
+++ b/src/com/android/settings/location/RecentLocationApps.java
@@ -31,6 +31,8 @@
 import android.os.UserManager;
 import android.preference.Preference;
 import android.util.Log;
+
+import com.android.settings.DimmableIconPreference;
 import com.android.settings.R;
 import com.android.settings.SettingsActivity;
 import com.android.settings.applications.InstalledAppDetails;
diff --git a/src/com/android/settings/location/SettingsInjector.java b/src/com/android/settings/location/SettingsInjector.java
index ef6117b..283430e 100644
--- a/src/com/android/settings/location/SettingsInjector.java
+++ b/src/com/android/settings/location/SettingsInjector.java
@@ -39,6 +39,8 @@
 import android.util.Log;
 import android.util.Xml;
 
+import com.android.settings.DimmableIconPreference;
+
 import org.xmlpull.v1.XmlPullParser;
 import org.xmlpull.v1.XmlPullParserException;
 
diff --git a/src/com/android/settings/users/UserSettings.java b/src/com/android/settings/users/UserSettings.java
index d5244d7..4c26c5c 100644
--- a/src/com/android/settings/users/UserSettings.java
+++ b/src/com/android/settings/users/UserSettings.java
@@ -818,12 +818,31 @@
         // Append Add user to the end of the list
         if (mUserCaps.mCanAddUser) {
             boolean moreUsers = mUserManager.canAddMoreUsers();
-            mAddUser.setEnabled(moreUsers);
             mAddUser.setOrder(Preference.DEFAULT_ORDER);
             preferenceScreen.addPreference(mAddUser);
+            mAddUser.setEnabled(moreUsers);
+            if (!moreUsers) {
+                mAddUser.setSummary(getString(R.string.user_add_max_count, getMaxRealUsers()));
+            } else {
+                mAddUser.setSummary(null);
+            }
         }
     }
 
+    private int getMaxRealUsers() {
+        // guest is not counted against getMaxSupportedUsers() number
+        final int maxUsersAndGuest = UserManager.getMaxSupportedUsers() + 1;
+        final List<UserInfo> users = mUserManager.getUsers();
+        // managed profiles are counted against getMaxSupportedUsers()
+        int managedProfiles = 0;
+        for (UserInfo user : users) {
+            if (user.isManagedProfile()) {
+                managedProfiles++;
+            }
+        }
+        return maxUsersAndGuest - managedProfiles;
+    }
+
     private boolean shouldShowGuestUserPreference(List<UserInfo> users) {
         boolean showGuestPreference = !mUserCaps.mIsGuest;
         // If user has DISALLOW_ADD_USER don't allow creating a guest either.