1 /*
2  * Copyright (C) 2018 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.car.settings.users;
18 
19 import android.car.drivingstate.CarUxRestrictions;
20 import android.os.Bundle;
21 
22 import androidx.annotation.LayoutRes;
23 import androidx.annotation.StringRes;
24 import androidx.recyclerview.widget.GridLayoutManager;
25 
26 import com.android.car.settings.R;
27 import com.android.car.settings.common.BaseFragment;
28 import com.android.car.settings.common.CarUxRestrictionsHelper;
29 
30 /**
31  * Shows a user switcher for all Users available on this device.
32  */
33 public class UserSwitcherFragment extends BaseFragment {
34 
35     private UserGridRecyclerView mUserGridView;
36 
37     @Override
38     @LayoutRes
getLayoutId()39     protected int getLayoutId() {
40         return R.layout.user_switcher;
41     }
42 
43     @Override
44     @StringRes
getTitleId()45     protected int getTitleId() {
46         return R.string.users_list_title;
47     }
48 
49     @Override
onActivityCreated(Bundle savedInstanceState)50     public void onActivityCreated(Bundle savedInstanceState) {
51         super.onActivityCreated(savedInstanceState);
52 
53         mUserGridView = getView().findViewById(R.id.user_grid);
54         GridLayoutManager layoutManager = new GridLayoutManager(getContext(),
55                 getContext().getResources().getInteger(R.integer.user_switcher_num_col));
56         mUserGridView.setFragment(this);
57         mUserGridView.setLayoutManager(layoutManager);
58         mUserGridView.buildAdapter();
59     }
60 
61     @Override
onDestroy()62     public void onDestroy() {
63         super.onDestroy();
64     }
65 
66     /**
67      * User switcher fragment is distraction optimized, so is allowed at all times.
68      */
69     @Override
canBeShown(CarUxRestrictions carUxRestrictions)70     public boolean canBeShown(CarUxRestrictions carUxRestrictions) {
71         return true;
72     }
73 
74 
75     @Override
onUxRestrictionsChanged(CarUxRestrictions restrictionInfo)76     public void onUxRestrictionsChanged(CarUxRestrictions restrictionInfo) {
77         applyRestriction(CarUxRestrictionsHelper.isNoSetup(restrictionInfo));
78     }
79 
applyRestriction(boolean restricted)80     private void applyRestriction(boolean restricted) {
81         if (mUserGridView != null) {
82             if (restricted) {
83                 mUserGridView.disableAddUser();
84             } else {
85                 mUserGridView.enableAddUser();
86             }
87         }
88     }
89 }
90