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.ArrayList; 35 import java.util.Arrays; 36 import java.util.Collections; 37 import java.util.List; 38 import java.util.stream.Collectors; 39 40 /** 41 * Manipulate users in various ways 42 */ 43 public final class UserRestrictionsFragment extends Fragment { 44 45 private static final String TAG = UserRestrictionsFragment.class.getSimpleName(); 46 47 private static final List<String> CONFIGURABLE_USER_RESTRICTIONS = 48 Arrays.asList( 49 UserManager.DISALLOW_ADD_USER, 50 UserManager.DISALLOW_BLUETOOTH, 51 UserManager.DISALLOW_CONFIG_BRIGHTNESS, 52 UserManager.DISALLOW_FACTORY_RESET, 53 UserManager.DISALLOW_INSTALL_APPS, 54 UserManager.DISALLOW_MODIFY_ACCOUNTS, 55 UserManager.DISALLOW_OUTGOING_CALLS, 56 UserManager.DISALLOW_REMOVE_USER, 57 UserManager.DISALLOW_SMS, 58 UserManager.DISALLOW_UNINSTALL_APPS, 59 UserManager.DISALLOW_USER_SWITCH 60 ); 61 62 static { 63 Collections.sort(CONFIGURABLE_USER_RESTRICTIONS); Log.d(TAG, "Configurable user restrictions: " + CONFIGURABLE_USER_RESTRICTIONS)64 Log.d(TAG, "Configurable user restrictions: " + CONFIGURABLE_USER_RESTRICTIONS); 65 } 66 67 @Nullable 68 @Override onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)69 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, 70 @Nullable Bundle savedInstanceState) { 71 return inflater.inflate(R.layout.user_restrictions, container, false); 72 } 73 onViewCreated(View view, Bundle savedInstanceState)74 public void onViewCreated(View view, Bundle savedInstanceState) { 75 ListView userRestrictionsList = view.findViewById(R.id.user_restrictions_list); 76 userRestrictionsList.setAdapter( 77 new UserRestrictionAdapter(getContext(), createUserRestrictionItems())); 78 79 Button applyButton = view.findViewById(R.id.apply_button); 80 applyButton.setOnClickListener(v -> { 81 UserRestrictionAdapter adapter = 82 (UserRestrictionAdapter) userRestrictionsList.getAdapter(); 83 int count = adapter.getCount(); 84 UserManager userManager = getUserManager(); 85 86 // Iterate through all of the user restrictions and set their values 87 List<String> restrictions = new ArrayList<>(count); 88 for (int i = 0; i < count; i++) { 89 UserRestrictionListItem item = (UserRestrictionListItem) adapter.getItem(i); 90 String restriction = item.getKey(); 91 boolean added = item.isChecked(); 92 userManager.setUserRestriction(restriction, added); 93 if (added) { 94 restrictions.add(restriction); 95 } 96 } 97 toast("%d restrictions (%s) have been set on user %d!", restrictions.size(), 98 restrictions, getContext().getUserId()); 99 }); 100 } 101 toast(String format, Object...args)102 private void toast(String format, Object...args) { 103 String msg = String.format(format, args); 104 Log.i(TAG, msg); 105 Toast.makeText(getContext(), msg, Toast.LENGTH_SHORT).show(); 106 } 107 createUserRestrictionItems()108 private List<UserRestrictionListItem> createUserRestrictionItems() { 109 int userId = getContext().getUser().getIdentifier(); 110 UserHandle user = UserHandle.of(userId); 111 UserManager userManager = getUserManager(); 112 113 List<UserRestrictionListItem> list = CONFIGURABLE_USER_RESTRICTIONS.stream() 114 .map((key) -> new UserRestrictionListItem(key, 115 userManager.hasBaseUserRestriction(key, user))) 116 .collect(Collectors.toList()); 117 Log.d(TAG, "Current restrictions for user " + userId + ": " + list); 118 119 return list; 120 } 121 getUserManager()122 private UserManager getUserManager() { 123 return getContext().getSystemService(UserManager.class); 124 } 125 } 126