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 import android.support.v7.widget.GridLayoutManager; 22 23 import com.android.car.settings.R; 24 import com.android.car.settings.common.BaseFragment; 25 import com.android.car.settings.common.CarUxRestrictionsHelper; 26 27 /** 28 * Shows a user switcher for all Users available on this device. 29 */ 30 public class UserSwitcherFragment extends BaseFragment { 31 32 private UserGridRecyclerView mUserGridView; 33 34 /** 35 * Initializes the UserSwitcherFragment 36 * @return instance of the UserSwitcherFragment 37 */ newInstance()38 public static UserSwitcherFragment newInstance() { 39 UserSwitcherFragment userSwitcherFragment = new UserSwitcherFragment(); 40 Bundle bundle = BaseFragment.getBundle(); 41 bundle.putInt(EXTRA_TITLE_ID, R.string.users_list_title); 42 bundle.putInt(EXTRA_ACTION_BAR_LAYOUT, R.layout.action_bar_with_button); 43 bundle.putInt(EXTRA_LAYOUT, R.layout.car_user_switcher); 44 userSwitcherFragment.setArguments(bundle); 45 return userSwitcherFragment; 46 } 47 48 @Override onActivityCreated(Bundle savedInstanceState)49 public void onActivityCreated(Bundle savedInstanceState) { 50 super.onActivityCreated(savedInstanceState); 51 52 mUserGridView = getView().findViewById(R.id.user_grid); 53 GridLayoutManager layoutManager = new GridLayoutManager(getContext(), 54 getContext().getResources().getInteger(R.integer.user_switcher_num_col)); 55 mUserGridView.setFragment(this); 56 mUserGridView.getRecyclerView().setLayoutManager(layoutManager); 57 mUserGridView.buildAdapter(); 58 } 59 60 @Override onDestroy()61 public void onDestroy() { 62 super.onDestroy(); 63 } 64 65 /** 66 * User switcher fragment is distraction optimized, so is allowed at all times. 67 */ 68 @Override canBeShown(CarUxRestrictions carUxRestrictions)69 public boolean canBeShown(CarUxRestrictions carUxRestrictions) { 70 return true; 71 } 72 73 74 @Override onUxRestrictionChanged(CarUxRestrictions carUxRestrictions)75 public void onUxRestrictionChanged(CarUxRestrictions carUxRestrictions) { 76 applyRestriction(CarUxRestrictionsHelper.isNoSetup(carUxRestrictions)); 77 } 78 applyRestriction(boolean restricted)79 private void applyRestriction(boolean restricted) { 80 if (mUserGridView != null) { 81 if (restricted) { 82 mUserGridView.disableAddUser(); 83 } else { 84 mUserGridView.enableAddUser(); 85 } 86 } 87 } 88 89 } 90