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.support.v7.preference.Preference; 24 import android.support.v7.preference.PreferenceViewHolder; 25 import android.util.AttributeSet; 26 import android.view.View; 27 import android.view.View.OnClickListener; 28 import android.widget.ImageView; 29 30 import com.android.settings.R; 31 import com.android.settingslib.RestrictedLockUtils; 32 import com.android.settingslib.RestrictedPreference; 33 34 import java.util.Comparator; 35 36 public class UserPreference extends RestrictedPreference { 37 private static final int ALPHA_ENABLED = 255; 38 private static final int ALPHA_DISABLED = 102; 39 40 public static final int USERID_UNKNOWN = -10; 41 public static final int USERID_GUEST_DEFAULTS = -11; 42 public static final Comparator<UserPreference> SERIAL_NUMBER_COMPARATOR = 43 new Comparator<UserPreference>() { 44 @Override 45 public int compare(UserPreference p1, UserPreference p2) { 46 int sn1 = p1.getSerialNumber(); 47 int sn2 = p2.getSerialNumber(); 48 if (sn1 < sn2) { 49 return -1; 50 } else if (sn1 > sn2) { 51 return 1; 52 } 53 return 0; 54 } 55 }; 56 57 private OnClickListener mDeleteClickListener; 58 private OnClickListener mSettingsClickListener; 59 private int mSerialNumber = -1; 60 private int mUserId = USERID_UNKNOWN; 61 static final int SETTINGS_ID = R.id.manage_user; 62 static final int DELETE_ID = R.id.trash_user; 63 UserPreference(Context context, AttributeSet attrs)64 public UserPreference(Context context, AttributeSet attrs) { 65 this(context, attrs, USERID_UNKNOWN, null, null); 66 } 67 UserPreference(Context context, AttributeSet attrs, int userId, OnClickListener settingsListener, OnClickListener deleteListener)68 UserPreference(Context context, AttributeSet attrs, int userId, 69 OnClickListener settingsListener, 70 OnClickListener deleteListener) { 71 super(context, attrs); 72 if (deleteListener != null || settingsListener != null) { 73 setWidgetLayoutResource(R.layout.restricted_preference_user_delete_widget); 74 } 75 mDeleteClickListener = deleteListener; 76 mSettingsClickListener = settingsListener; 77 mUserId = userId; 78 useAdminDisabledSummary(true); 79 } 80 dimIcon(boolean dimmed)81 private void dimIcon(boolean dimmed) { 82 Drawable icon = getIcon(); 83 if (icon != null) { 84 icon.mutate().setAlpha(dimmed ? ALPHA_DISABLED : ALPHA_ENABLED); 85 setIcon(icon); 86 } 87 } 88 89 @Override onBindViewHolder(PreferenceViewHolder view)90 public void onBindViewHolder(PreferenceViewHolder view) { 91 super.onBindViewHolder(view); 92 final boolean disabledByAdmin = isDisabledByAdmin(); 93 dimIcon(disabledByAdmin); 94 View userDeleteWidget = view.findViewById(R.id.user_delete_widget); 95 if (userDeleteWidget != null) { 96 userDeleteWidget.setVisibility(disabledByAdmin ? View.GONE : View.VISIBLE); 97 } 98 if (!disabledByAdmin) { 99 UserManager um = (UserManager) getContext().getSystemService(Context.USER_SERVICE); 100 View deleteDividerView = view.findViewById(R.id.divider_delete); 101 View manageDividerView = view.findViewById(R.id.divider_manage); 102 View deleteView = view.findViewById(R.id.trash_user); 103 if (deleteView != null) { 104 if (mDeleteClickListener != null 105 && !RestrictedLockUtils.hasBaseUserRestriction(getContext(), 106 UserManager.DISALLOW_REMOVE_USER, UserHandle.myUserId())) { 107 deleteView.setVisibility(View.VISIBLE); 108 deleteDividerView.setVisibility(View.VISIBLE); 109 deleteView.setOnClickListener(mDeleteClickListener); 110 deleteView.setTag(this); 111 } else { 112 deleteView.setVisibility(View.GONE); 113 deleteDividerView.setVisibility(View.GONE); 114 } 115 } 116 ImageView manageView = (ImageView) view.findViewById(R.id.manage_user); 117 if (manageView != null) { 118 if (mSettingsClickListener != null) { 119 manageView.setVisibility(View.VISIBLE); 120 manageDividerView.setVisibility(mDeleteClickListener == null 121 ? View.VISIBLE : View.GONE); 122 manageView.setOnClickListener(mSettingsClickListener); 123 manageView.setTag(this); 124 } else { 125 manageView.setVisibility(View.GONE); 126 manageDividerView.setVisibility(View.GONE); 127 } 128 } 129 } 130 } 131 getSerialNumber()132 private int getSerialNumber() { 133 if (mUserId == UserHandle.myUserId()) return Integer.MIN_VALUE; 134 if (mSerialNumber < 0) { 135 // If the userId is unknown 136 if (mUserId == USERID_UNKNOWN) { 137 return Integer.MAX_VALUE; 138 } else if (mUserId == USERID_GUEST_DEFAULTS) { 139 return Integer.MAX_VALUE - 1; 140 } 141 mSerialNumber = ((UserManager) getContext().getSystemService(Context.USER_SERVICE)) 142 .getUserSerialNumber(mUserId); 143 if (mSerialNumber < 0) return mUserId; 144 } 145 return mSerialNumber; 146 } 147 getUserId()148 public int getUserId() { 149 return mUserId; 150 } 151 } 152