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