1 /* 2 * Copyright (C) 2012 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.settings.users; 18 19 import android.content.Context; 20 import android.graphics.drawable.Drawable; 21 import android.os.UserHandle; 22 import android.os.UserManager; 23 import android.util.AttributeSet; 24 25 import androidx.preference.PreferenceViewHolder; 26 27 import com.android.settingslib.RestrictedPreference; 28 29 import java.util.Comparator; 30 31 /** 32 * Preference for a user that appear on {@link UserSettings} screen. 33 */ 34 public class UserPreference extends RestrictedPreference { 35 private static final int ALPHA_ENABLED = 255; 36 private static final int ALPHA_DISABLED = 102; 37 38 public static final int USERID_UNKNOWN = -10; 39 public static final Comparator<UserPreference> SERIAL_NUMBER_COMPARATOR = 40 (p1, p2) -> { 41 42 if (p1 == null) { 43 return -1; 44 } else if (p2 == null) { 45 return 1; 46 } 47 int sn1 = p1.getSerialNumber(); 48 int sn2 = p2.getSerialNumber(); 49 if (sn1 < sn2) { 50 return -1; 51 } else if (sn1 > sn2) { 52 return 1; 53 } 54 return 0; 55 }; 56 57 private int mSerialNumber = -1; 58 private int mUserId = USERID_UNKNOWN; 59 UserPreference(Context context, AttributeSet attrs)60 public UserPreference(Context context, AttributeSet attrs) { 61 this(context, attrs, USERID_UNKNOWN); 62 } 63 UserPreference(Context context, AttributeSet attrs, int userId)64 UserPreference(Context context, AttributeSet attrs, int userId) { 65 super(context, attrs); 66 mUserId = userId; 67 useAdminDisabledSummary(true); 68 } 69 dimIcon(boolean dimmed)70 private void dimIcon(boolean dimmed) { 71 Drawable icon = getIcon(); 72 if (icon != null) { 73 icon.mutate().setAlpha(dimmed ? ALPHA_DISABLED : ALPHA_ENABLED); 74 setIcon(icon); 75 } 76 } 77 78 @Override shouldHideSecondTarget()79 protected boolean shouldHideSecondTarget() { 80 return true; 81 } 82 83 @Override onBindViewHolder(PreferenceViewHolder view)84 public void onBindViewHolder(PreferenceViewHolder view) { 85 super.onBindViewHolder(view); 86 dimIcon(isDisabledByAdmin()); 87 } 88 getSerialNumber()89 private int getSerialNumber() { 90 if (mUserId == UserHandle.myUserId()) return Integer.MIN_VALUE; 91 if (mSerialNumber < 0) { 92 // If the userId is unknown 93 if (mUserId == USERID_UNKNOWN) { 94 return Integer.MAX_VALUE; 95 } 96 mSerialNumber = ((UserManager) getContext().getSystemService(Context.USER_SERVICE)) 97 .getUserSerialNumber(mUserId); 98 if (mSerialNumber < 0) return mUserId; 99 } 100 return mSerialNumber; 101 } 102 getUserId()103 public int getUserId() { 104 return mUserId; 105 } 106 } 107