Aarthi Balachander | 7109676 | 2018-05-21 18:22:23 -0700 | [diff] [blame] | 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; |
Lujiang Xue | 8937ba5 | 2018-06-06 10:36:17 -0700 | [diff] [blame] | 21 | |
davidln | cb0bfa2 | 2018-08-27 14:13:05 -0700 | [diff] [blame] | 22 | import androidx.annotation.LayoutRes; |
| 23 | import androidx.annotation.StringRes; |
Lujiang Xue | 8937ba5 | 2018-06-06 10:36:17 -0700 | [diff] [blame] | 24 | import androidx.recyclerview.widget.GridLayoutManager; |
Aarthi Balachander | 7109676 | 2018-05-21 18:22:23 -0700 | [diff] [blame] | 25 | |
| 26 | import com.android.car.settings.R; |
| 27 | import com.android.car.settings.common.BaseFragment; |
Aarthi Balachander | b88fd9b | 2018-05-30 16:25:16 -0700 | [diff] [blame] | 28 | import com.android.car.settings.common.CarUxRestrictionsHelper; |
Aarthi Balachander | 7109676 | 2018-05-21 18:22:23 -0700 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * Shows a user switcher for all Users available on this device. |
| 32 | */ |
| 33 | public class UserSwitcherFragment extends BaseFragment { |
| 34 | |
Aarthi Balachander | b88fd9b | 2018-05-30 16:25:16 -0700 | [diff] [blame] | 35 | private UserGridRecyclerView mUserGridView; |
| 36 | |
davidln | cb0bfa2 | 2018-08-27 14:13:05 -0700 | [diff] [blame] | 37 | @Override |
| 38 | @LayoutRes |
| 39 | protected int getActionBarLayoutId() { |
| 40 | return R.layout.action_bar_with_button; |
| 41 | } |
| 42 | |
| 43 | @Override |
| 44 | @LayoutRes |
| 45 | protected int getLayoutId() { |
davidln | 14aa507 | 2019-01-03 11:27:16 -0800 | [diff] [blame] | 46 | return R.layout.user_switcher; |
davidln | cb0bfa2 | 2018-08-27 14:13:05 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | @Override |
| 50 | @StringRes |
| 51 | protected int getTitleId() { |
| 52 | return R.string.users_list_title; |
Aarthi Balachander | 7109676 | 2018-05-21 18:22:23 -0700 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | @Override |
| 56 | public void onActivityCreated(Bundle savedInstanceState) { |
| 57 | super.onActivityCreated(savedInstanceState); |
| 58 | |
Aarthi Balachander | b88fd9b | 2018-05-30 16:25:16 -0700 | [diff] [blame] | 59 | mUserGridView = getView().findViewById(R.id.user_grid); |
Aarthi Balachander | 7109676 | 2018-05-21 18:22:23 -0700 | [diff] [blame] | 60 | GridLayoutManager layoutManager = new GridLayoutManager(getContext(), |
| 61 | getContext().getResources().getInteger(R.integer.user_switcher_num_col)); |
| 62 | mUserGridView.setFragment(this); |
davidln | f1494d8 | 2019-03-08 16:35:55 -0800 | [diff] [blame] | 63 | mUserGridView.setLayoutManager(layoutManager); |
Aarthi Balachander | 7109676 | 2018-05-21 18:22:23 -0700 | [diff] [blame] | 64 | mUserGridView.buildAdapter(); |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public void onDestroy() { |
| 69 | super.onDestroy(); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * User switcher fragment is distraction optimized, so is allowed at all times. |
| 74 | */ |
| 75 | @Override |
| 76 | public boolean canBeShown(CarUxRestrictions carUxRestrictions) { |
| 77 | return true; |
| 78 | } |
| 79 | |
Aarthi Balachander | b88fd9b | 2018-05-30 16:25:16 -0700 | [diff] [blame] | 80 | |
| 81 | @Override |
davidln | f975729 | 2018-09-17 09:52:47 -0700 | [diff] [blame] | 82 | public void onUxRestrictionsChanged(CarUxRestrictions restrictionInfo) { |
| 83 | applyRestriction(CarUxRestrictionsHelper.isNoSetup(restrictionInfo)); |
Aarthi Balachander | b88fd9b | 2018-05-30 16:25:16 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | private void applyRestriction(boolean restricted) { |
| 87 | if (mUserGridView != null) { |
| 88 | if (restricted) { |
Lujiang Xue | 8601fa3 | 2018-05-31 13:59:08 -0700 | [diff] [blame] | 89 | mUserGridView.disableAddUser(); |
Aarthi Balachander | b88fd9b | 2018-05-30 16:25:16 -0700 | [diff] [blame] | 90 | } else { |
Lujiang Xue | 8601fa3 | 2018-05-31 13:59:08 -0700 | [diff] [blame] | 91 | mUserGridView.enableAddUser(); |
Aarthi Balachander | b88fd9b | 2018-05-30 16:25:16 -0700 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
Aarthi Balachander | 7109676 | 2018-05-21 18:22:23 -0700 | [diff] [blame] | 96 | } |