1 /*
2  * Copyright (C) 2019 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 package com.google.android.car.kitchensink.users;
17 
18 import android.annotation.Nullable;
19 import android.content.Context;
20 import android.os.Bundle;
21 import android.os.UserManager;
22 import android.view.LayoutInflater;
23 import android.view.View;
24 import android.view.ViewGroup;
25 import android.widget.Button;
26 import android.widget.ListView;
27 import android.widget.Toast;
28 
29 import androidx.fragment.app.Fragment;
30 
31 import com.google.android.car.kitchensink.R;
32 
33 import java.util.ArrayList;
34 import java.util.Arrays;
35 import java.util.List;
36 
37 /**
38  * Manipulate users in various ways
39  */
40 public class UsersFragment extends Fragment {
41 
42     private static final List<String> CONFIGURABLE_USER_RESTRICTIONS =
43             Arrays.asList(
44                     UserManager.DISALLOW_ADD_USER,
45                     UserManager.DISALLOW_BLUETOOTH,
46                     UserManager.DISALLOW_FACTORY_RESET,
47                     UserManager.DISALLOW_INSTALL_APPS,
48                     UserManager.DISALLOW_MODIFY_ACCOUNTS,
49                     UserManager.DISALLOW_OUTGOING_CALLS,
50                     UserManager.DISALLOW_REMOVE_USER,
51                     UserManager.DISALLOW_SMS,
52                     UserManager.DISALLOW_UNINSTALL_APPS,
53                     UserManager.DISALLOW_USER_SWITCH
54             );
55 
56     @Nullable
57     @Override
onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)58     public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
59             @Nullable Bundle savedInstanceState) {
60         return inflater.inflate(R.layout.users, container, false);
61     }
62 
onViewCreated(View view, Bundle savedInstanceState)63     public void onViewCreated(View view, Bundle savedInstanceState) {
64         ListView userRestrictionsList = view.findViewById(R.id.user_restrictions_list);
65         userRestrictionsList.setAdapter(
66                 new UserRestrictionAdapter(getContext(), createUserRestrictionItems()));
67 
68         Button applyButton = view.findViewById(R.id.apply_button);
69         applyButton.setOnClickListener(v -> {
70             UserRestrictionAdapter adapter =
71                     (UserRestrictionAdapter) userRestrictionsList.getAdapter();
72             int count = adapter.getCount();
73             UserManager userManager =
74                     (UserManager) getContext().getSystemService(Context.USER_SERVICE);
75 
76             // Iterate through all of the user restrictions and set their values
77             for (int i = 0; i < count; i++) {
78                 UserRestrictionListItem item = (UserRestrictionListItem) adapter.getItem(i);
79                 userManager.setUserRestriction(item.getKey(), item.getIsChecked());
80             }
81 
82             Toast.makeText(
83                     getContext(), "User restrictions have been set!", Toast.LENGTH_SHORT)
84                     .show();
85         });
86     }
87 
createUserRestrictionItems()88     private List<UserRestrictionListItem> createUserRestrictionItems() {
89         UserManager userManager = (UserManager) getContext().getSystemService(Context.USER_SERVICE);
90         ArrayList<UserRestrictionListItem> list = new ArrayList<>();
91         for (String key : CONFIGURABLE_USER_RESTRICTIONS) {
92             list.add(new UserRestrictionListItem(key, userManager.hasUserRestriction(key)));
93         }
94         return list;
95     }
96 }
97