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 int USERID_GUEST_DEFAULTS = -11; 40 public static final Comparator<UserPreference> SERIAL_NUMBER_COMPARATOR = 41 (p1, p2) -> { 42 43 if (p1 == null) { 44 return -1; 45 } else if (p2 == null) { 46 return 1; 47 } 48 int sn1 = p1.getSerialNumber(); 49 int sn2 = p2.getSerialNumber(); 50 if (sn1 < sn2) { 51 return -1; 52 } else if (sn1 > sn2) { 53 return 1; 54 } 55 return 0; 56 }; 57 58 private int mSerialNumber = -1; 59 private int mUserId = USERID_UNKNOWN; 60 UserPreference(Context context, AttributeSet attrs)61 public UserPreference(Context context, AttributeSet attrs) { 62 this(context, attrs, USERID_UNKNOWN); 63 } 64 UserPreference(Context context, AttributeSet attrs, int userId)65 UserPreference(Context context, AttributeSet attrs, int userId) { 66 super(context, attrs); 67 mUserId = userId; 68 useAdminDisabledSummary(true); 69 } 70 dimIcon(boolean dimmed)71 private void dimIcon(boolean dimmed) { 72 Drawable icon = getIcon(); 73 if (icon != null) { 74 icon.mutate().setAlpha(dimmed ? ALPHA_DISABLED : ALPHA_ENABLED); 75 setIcon(icon); 76 } 77 } 78 79 @Override shouldHideSecondTarget()80 protected boolean shouldHideSecondTarget() { 81 return true; 82 } 83 84 @Override onBindViewHolder(PreferenceViewHolder view)85 public void onBindViewHolder(PreferenceViewHolder view) { 86 super.onBindViewHolder(view); 87 dimIcon(isDisabledByAdmin()); 88 } 89 getSerialNumber()90 private int getSerialNumber() { 91 if (mUserId == UserHandle.myUserId()) return Integer.MIN_VALUE; 92 if (mSerialNumber < 0) { 93 // If the userId is unknown 94 if (mUserId == USERID_UNKNOWN) { 95 return Integer.MAX_VALUE; 96 } else if (mUserId == USERID_GUEST_DEFAULTS) { 97 return Integer.MAX_VALUE - 1; 98 } 99 mSerialNumber = ((UserManager) getContext().getSystemService(Context.USER_SERVICE)) 100 .getUserSerialNumber(mUserId); 101 if (mSerialNumber < 0) return mUserId; 102 } 103 return mSerialNumber; 104 } 105 getUserId()106 public int getUserId() { 107 return mUserId; 108 } 109 } 110