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.os.Bundle;
20 import android.os.UserHandle;
21 import android.os.UserManager;
22 import android.util.Log;
23 import android.view.LayoutInflater;
24 import android.view.View;
25 import android.view.ViewGroup;
26 import android.widget.Button;
27 import android.widget.ListView;
28 import android.widget.Toast;
29 
30 import androidx.fragment.app.Fragment;
31 
32 import com.google.android.car.kitchensink.R;
33 
34 import java.util.Arrays;
35 import java.util.Collections;
36 import java.util.List;
37 import java.util.stream.Collectors;
38 
39 /**
40  * Manipulate users in various ways
41  */
42 public final class UserRestrictionsFragment extends Fragment {
43 
44     private static final String TAG = UserRestrictionsFragment.class.getSimpleName();
45 
46     private static final List<String> CONFIGURABLE_USER_RESTRICTIONS =
47             Arrays.asList(
48                     UserManager.DISALLOW_ADD_USER,
49                     UserManager.DISALLOW_BLUETOOTH,
50                     UserManager.DISALLOW_CONFIG_BRIGHTNESS,
51                     UserManager.DISALLOW_FACTORY_RESET,
52                     UserManager.DISALLOW_INSTALL_APPS,
53                     UserManager.DISALLOW_MODIFY_ACCOUNTS,
54                     UserManager.DISALLOW_OUTGOING_CALLS,
55                     UserManager.DISALLOW_REMOVE_USER,
56                     UserManager.DISALLOW_SMS,
57                     UserManager.DISALLOW_UNINSTALL_APPS,
58                     UserManager.DISALLOW_USER_SWITCH
59             );
60 
61     static {
62         Collections.sort(CONFIGURABLE_USER_RESTRICTIONS);
Log.d(TAG, R + CONFIGURABLE_USER_RESTRICTIONS)63         Log.d(TAG, "Configurable user restrictions: " + CONFIGURABLE_USER_RESTRICTIONS);
64     }
65 
66     @Nullable
67     @Override
onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)68     public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
69             @Nullable Bundle savedInstanceState) {
70         return inflater.inflate(R.layout.user_restrictions, container, false);
71     }
72 
onViewCreated(View view, Bundle savedInstanceState)73     public void onViewCreated(View view, Bundle savedInstanceState) {
74         ListView userRestrictionsList = view.findViewById(R.id.user_restrictions_list);
75         userRestrictionsList.setAdapter(
76                 new UserRestrictionAdapter(getContext(), createUserRestrictionItems()));
77 
78         Button applyButton = view.findViewById(R.id.apply_button);
79         applyButton.setOnClickListener(v -> {
80             UserRestrictionAdapter adapter =
81                     (UserRestrictionAdapter) userRestrictionsList.getAdapter();
82             int count = adapter.getCount();
83             UserManager userManager = getUserManager();
84 
85             // Iterate through all of the user restrictions and set their values
86             for (int i = 0; i < count; i++) {
87                 UserRestrictionListItem item = (UserRestrictionListItem) adapter.getItem(i);
88                 userManager.setUserRestriction(item.getKey(), item.getIsChecked());
89             }
90 
91             Toast.makeText(
92                     getContext(), "User restrictions have been set!", Toast.LENGTH_SHORT)
93                     .show();
94         });
95     }
96 
createUserRestrictionItems()97     private List<UserRestrictionListItem> createUserRestrictionItems() {
98         int userId = getContext().getUserId();
99         UserHandle user = UserHandle.of(userId);
100         UserManager userManager = getUserManager();
101 
102         List<UserRestrictionListItem> list = CONFIGURABLE_USER_RESTRICTIONS.stream()
103                 .map((key) -> new UserRestrictionListItem(key,
104                         userManager.hasBaseUserRestriction(key, user)))
105                 .collect(Collectors.toList());
106         Log.d(TAG, "Current restrictions for user " + userId + ": " + list);
107 
108         return list;
109     }
110 
getUserManager()111     private UserManager getUserManager() {
112         return getContext().getSystemService(UserManager.class);
113     }
114 }
115