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 static java.util.Objects.requireNonNull;
20 
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.pm.UserInfo;
24 import android.os.Bundle;
25 
26 import androidx.annotation.XmlRes;
27 
28 import com.android.car.settings.R;
29 import com.android.car.settings.common.SettingsFragment;
30 import com.android.car.ui.toolbar.MenuItem;
31 
32 import java.util.Collections;
33 import java.util.List;
34 
35 /**
36  * This screen appears after the last admin on the device tries to delete themselves. (but only if
37  * there are other users on the device)
38  *
39  * <p> It lets the Admin see a list of non-Admins on the device and choose a user from the list to
40  * upgrade to Admin.
41  *
42  * <p> After new admin has been selected and upgraded, the old Admin is removed.
43  */
44 public class ChooseNewAdminFragment extends SettingsFragment {
45 
46     private MenuItem mCancelButton;
47 
48     /**
49      * Creates a new instance of {@link ChooseNewAdminFragment} that enables the last remaining
50      * admin to choose a new Admin from a list of Non-Admins.
51      *
52      * @param adminInfo Admin that will get removed after new admin has been designated.
53      */
newInstance(UserInfo adminInfo)54     public static ChooseNewAdminFragment newInstance(UserInfo adminInfo) {
55         ChooseNewAdminFragment usersListFragment = new ChooseNewAdminFragment();
56         Bundle bundle = new Bundle();
57         bundle.putParcelable(Intent.EXTRA_USER, adminInfo);
58         usersListFragment.setArguments(bundle);
59         return usersListFragment;
60     }
61 
62     @Override
getToolbarMenuItems()63     public List<MenuItem> getToolbarMenuItems() {
64         return Collections.singletonList(mCancelButton);
65     }
66 
67     @Override
onCreate(Bundle savedInstanceState)68     public void onCreate(Bundle savedInstanceState) {
69         super.onCreate(savedInstanceState);
70 
71         mCancelButton = new MenuItem.Builder(getContext())
72                 .setTitle(R.string.cancel)
73                 .setOnClickListener(i -> requireActivity().onBackPressed())
74                 .build();
75     }
76 
77     @Override
78     @XmlRes
getPreferenceScreenResId()79     protected int getPreferenceScreenResId() {
80         return R.xml.choose_new_admin_fragment;
81     }
82 
83     @Override
onAttach(Context context)84     public void onAttach(Context context) {
85         super.onAttach(context);
86         UserInfo adminInfo = requireNonNull(getArguments()).getParcelable(
87                 Intent.EXTRA_USER);
88         use(ChooseNewAdminPreferenceController.class, R.string.pk_choose_new_admin).setAdminInfo(
89                 adminInfo);
90     }
91 }
92