1 /*
2  * Copyright (C) 2020 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 package com.google.android.car.kitchensink.users;
17 
18 import android.annotation.NonNull;
19 import android.annotation.UserIdInt;
20 import android.content.Context;
21 import android.content.pm.UserInfo;
22 import android.os.UserManager;
23 import android.util.AttributeSet;
24 import android.widget.Button;
25 import android.widget.LinearLayout;
26 import android.widget.TextView;
27 
28 import com.google.android.car.kitchensink.R;
29 
30 import java.util.List;
31 
32 /**
33  * Custom {@link android.view.View} that shows the existing Android users.
34  */
35 public final class ExistingUsersView extends LinearLayout {
36 
37     private UserManager mUserManager;
38 
39     private TextView mNumberUsers;
40     private UsersSpinner mExistingUsers;
41     private UserInfoView mSelectedUser;
42     private Button mUpdateButton;
43 
ExistingUsersView(Context context, AttributeSet attrs)44     public ExistingUsersView(Context context, AttributeSet attrs) {
45         super(context, attrs);
46 
47         mUserManager = UserManager.get(context);
48 
49         inflate(context, R.layout.existing_users_view, this);
50         mNumberUsers = findViewById(R.id.number_users);
51         mExistingUsers = findViewById(R.id.existing_users);
52         mSelectedUser = findViewById(R.id.selected_user);
53         mUpdateButton = findViewById(R.id.update);
54 
55         mExistingUsers.setOnUserSelectedListener((u) -> updateUser(u));
56         mUpdateButton.setOnClickListener((v) -> updateState());
57 
58         updateState();
59     }
60 
updateUser(@onNull UserInfo user)61     private void updateUser(@NonNull UserInfo user) {
62         mSelectedUser.update(user);
63     }
64 
updateState()65     public void updateState() {
66         List<UserInfo> users = mUserManager.getUsers(/*excludePartial= */ false,
67                 /* excludeDying= */ false, /* excludePreCreated= */ false);
68         mNumberUsers.setText(users.size() + " existing users");
69         mExistingUsers.init(users);
70         updateUser(mExistingUsers.getSelectedUser());
71     }
72 
getSelectedUserId()73     public @UserIdInt int getSelectedUserId() {
74         return mExistingUsers.getSelectedUserId();
75     }
76 }
77