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 com.android.settings.R; 20 21 import android.content.Context; 22 import android.os.UserHandle; 23 import android.os.UserManager; 24 import android.preference.Preference; 25 import android.util.AttributeSet; 26 import android.view.View; 27 import android.view.View.OnClickListener; 28 29 public class UserPreference extends Preference { 30 31 public static final int USERID_UNKNOWN = -10; 32 public static final int USERID_GUEST_DEFAULTS = -11; 33 34 private OnClickListener mDeleteClickListener; 35 private OnClickListener mSettingsClickListener; 36 private int mSerialNumber = -1; 37 private int mUserId = USERID_UNKNOWN; 38 static final int SETTINGS_ID = R.id.manage_user; 39 static final int DELETE_ID = R.id.trash_user; 40 UserPreference(Context context, AttributeSet attrs)41 public UserPreference(Context context, AttributeSet attrs) { 42 this(context, attrs, USERID_UNKNOWN, null, null); 43 } 44 UserPreference(Context context, AttributeSet attrs, int userId, OnClickListener settingsListener, OnClickListener deleteListener)45 UserPreference(Context context, AttributeSet attrs, int userId, 46 OnClickListener settingsListener, 47 OnClickListener deleteListener) { 48 super(context, attrs); 49 if (deleteListener != null || settingsListener != null) { 50 setWidgetLayoutResource(R.layout.preference_user_delete_widget); 51 } 52 mDeleteClickListener = deleteListener; 53 mSettingsClickListener = settingsListener; 54 mUserId = userId; 55 } 56 57 @Override onBindView(View view)58 protected void onBindView(View view) { 59 UserManager um = (UserManager) getContext().getSystemService(Context.USER_SERVICE); 60 View deleteDividerView = view.findViewById(R.id.divider_delete); 61 View manageDividerView = view.findViewById(R.id.divider_manage); 62 View deleteView = view.findViewById(R.id.trash_user); 63 if (deleteView != null) { 64 if (mDeleteClickListener != null 65 && !um.hasUserRestriction(UserManager.DISALLOW_REMOVE_USER)) { 66 deleteView.setOnClickListener(mDeleteClickListener); 67 deleteView.setTag(this); 68 } else { 69 deleteView.setVisibility(View.GONE); 70 deleteDividerView.setVisibility(View.GONE); 71 } 72 } 73 View manageView = view.findViewById(R.id.manage_user); 74 if (manageView != null) { 75 if (mSettingsClickListener != null) { 76 manageView.setOnClickListener(mSettingsClickListener); 77 manageView.setTag(this); 78 if (mDeleteClickListener != null) { 79 manageDividerView.setVisibility(View.GONE); 80 } 81 } else { 82 manageView.setVisibility(View.GONE); 83 manageDividerView.setVisibility(View.GONE); 84 } 85 } 86 super.onBindView(view); 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 } else if (mUserId == USERID_GUEST_DEFAULTS) { 96 return Integer.MAX_VALUE - 1; 97 } 98 mSerialNumber = ((UserManager) getContext().getSystemService(Context.USER_SERVICE)) 99 .getUserSerialNumber(mUserId); 100 if (mSerialNumber < 0) return mUserId; 101 } 102 return mSerialNumber; 103 } 104 getUserId()105 public int getUserId() { 106 return mUserId; 107 } 108 compareTo(Preference another)109 public int compareTo(Preference another) { 110 if (another instanceof UserPreference) { 111 return getSerialNumber() > ((UserPreference) another).getSerialNumber() ? 1 : -1; 112 } else { 113 return 1; 114 } 115 } 116 } 117