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.user.CarUserManagerHelper; 20 import android.graphics.drawable.Drawable; 21 import android.os.Bundle; 22 import android.view.View; 23 import android.widget.Button; 24 25 import androidx.car.widget.ListItem; 26 import androidx.car.widget.ListItemProvider; 27 import androidx.car.widget.TextListItem; 28 29 import com.android.car.settings.R; 30 import com.android.car.settings.common.ListItemSettingsFragment; 31 32 import java.util.ArrayList; 33 import java.util.List; 34 35 /** 36 * Shows details for a guest session, including ability to switch to guest. 37 */ 38 public class GuestFragment extends ListItemSettingsFragment { 39 private CarUserManagerHelper mCarUserManagerHelper; 40 private ListItemProvider mItemProvider; 41 42 /** 43 * Create new GuestFragment instance. 44 */ newInstance()45 public static GuestFragment newInstance() { 46 GuestFragment guestFragment = new GuestFragment(); 47 Bundle bundle = ListItemSettingsFragment.getBundle(); 48 bundle.putInt(EXTRA_TITLE_ID, R.string.user_details_title); 49 bundle.putInt(EXTRA_ACTION_BAR_LAYOUT, R.layout.action_bar_with_button); 50 guestFragment.setArguments(bundle); 51 return guestFragment; 52 } 53 54 @Override onActivityCreated(Bundle savedInstanceState)55 public void onActivityCreated(Bundle savedInstanceState) { 56 mCarUserManagerHelper = new CarUserManagerHelper(getContext()); 57 mItemProvider = new ListItemProvider.ListProvider(getListItems()); 58 59 // Super class's onActivityCreated need to be called after mItemProvider is initialized. 60 // Because getItemProvider is called in there. 61 super.onActivityCreated(savedInstanceState); 62 63 showSwitchButton(); 64 } 65 showSwitchButton()66 private void showSwitchButton() { 67 Button switchUserBtn = (Button) getActivity().findViewById(R.id.action_button1); 68 // If the current process is not allowed to switch to another user, doe not show the switch 69 // button. 70 if (!mCarUserManagerHelper.canCurrentProcessSwitchUsers()) { 71 switchUserBtn.setVisibility(View.GONE); 72 return; 73 } 74 switchUserBtn.setVisibility(View.VISIBLE); 75 switchUserBtn.setText(R.string.user_switch); 76 switchUserBtn.setOnClickListener(v -> { 77 getActivity().onBackPressed(); 78 mCarUserManagerHelper.startNewGuestSession(getContext().getString(R.string.user_guest)); 79 }); 80 } 81 getListItems()82 private List<ListItem> getListItems() { 83 Drawable icon = UserIconProvider.scaleUserIcon(mCarUserManagerHelper.getGuestDefaultIcon(), 84 mCarUserManagerHelper, getContext()); 85 86 TextListItem item = new TextListItem(getContext()); 87 item.setPrimaryActionIcon(icon, /* useLargeIcon= */ false); 88 item.setTitle(getContext().getString(R.string.user_guest)); 89 90 List<ListItem> items = new ArrayList<>(); 91 items.add(item); 92 return items; 93 } 94 95 @Override getItemProvider()96 public ListItemProvider getItemProvider() { 97 return mItemProvider; 98 } 99 } 100