1 /* 2 * Copyright (C) 2023 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.tv.settings.system; 18 19 import android.app.admin.DevicePolicyManager; 20 import android.content.ComponentName; 21 import android.content.Context; 22 import android.os.Bundle; 23 import android.os.UserManager; 24 25 import androidx.annotation.Keep; 26 import androidx.preference.Preference; 27 import androidx.preference.PreferenceGroup; 28 29 import com.android.tv.settings.R; 30 import com.android.tv.settings.users.AppRestrictionsFragment; 31 import com.android.tv.settings.users.RestrictedProfileModel; 32 33 import java.util.List; 34 35 /** 36 * The security settings screen in Tv settings. 37 */ 38 @Keep 39 public class SecurityFragment extends BaseSecurityFragment { 40 private Preference mUnknownSourcesPref; 41 private PreferenceGroup mRestrictedProfileGroup; 42 private Preference mRestrictedProfileEnterPref; 43 private Preference mRestrictedProfileExitPref; 44 private Preference mRestrictedProfileAppsPref; 45 private Preference mRestrictedProfilePinPref; 46 private Preference mRestrictedProfileCreatePref; 47 private Preference mRestrictedProfileDeletePref; 48 49 private Preference mManageDeviceAdminPref; 50 private Preference mEnterprisePrivacyPref; 51 newInstance()52 public static SecurityFragment newInstance() { 53 return new SecurityFragment(); 54 } 55 56 /** 57 * Called by other Fragments to decide whether to show or hide profile-related views. 58 */ isRestrictedProfileInEffect(Context context)59 public static boolean isRestrictedProfileInEffect(Context context) { 60 return new RestrictedProfileModel(context).isCurrentUser(); 61 } 62 63 @Override onCreatePreferences(Bundle savedInstanceState, String rootKey)64 public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { 65 setPreferencesFromResource(R.xml.security, null); 66 67 mUnknownSourcesPref = findPreference(KEY_UNKNOWN_SOURCES); 68 mRestrictedProfileGroup = (PreferenceGroup) findPreference(KEY_RESTRICTED_PROFILE_GROUP); 69 mRestrictedProfileEnterPref = findPreference(KEY_RESTRICTED_PROFILE_ENTER); 70 mRestrictedProfileExitPref = findPreference(KEY_RESTRICTED_PROFILE_EXIT); 71 mRestrictedProfileAppsPref = findPreference(KEY_RESTRICTED_PROFILE_APPS); 72 mRestrictedProfilePinPref = findPreference(KEY_RESTRICTED_PROFILE_PIN); 73 mRestrictedProfileCreatePref = findPreference(KEY_RESTRICTED_PROFILE_CREATE); 74 mRestrictedProfileDeletePref = findPreference(KEY_RESTRICTED_PROFILE_DELETE); 75 76 mManageDeviceAdminPref = findPreference(KEY_MANAGE_DEVICE_ADMIN); 77 mEnterprisePrivacyPref = findPreference(KEY_ENTERPRISE_PRIVACY); 78 refresh(); 79 } 80 81 @Override refresh()82 protected void refresh() { 83 if (mRestrictedProfile.isCurrentUser()) { 84 // We are in restricted profile 85 mUnknownSourcesPref.setVisible(false); 86 87 mRestrictedProfileGroup.setVisible(true); 88 mRestrictedProfileEnterPref.setVisible(false); 89 mRestrictedProfileExitPref.setVisible(true); 90 mRestrictedProfileAppsPref.setVisible(false); 91 mRestrictedProfilePinPref.setVisible(false); 92 mRestrictedProfileCreatePref.setVisible(false); 93 mRestrictedProfileDeletePref.setVisible(false); 94 } else if (mRestrictedProfile.getUser() != null) { 95 // Not in restricted profile, but it exists 96 mUnknownSourcesPref.setVisible(true); 97 98 mRestrictedProfileGroup.setVisible(true); 99 mRestrictedProfileEnterPref.setVisible(true); 100 mRestrictedProfileExitPref.setVisible(false); 101 mRestrictedProfileAppsPref.setVisible(true); 102 mRestrictedProfilePinPref.setVisible(true); 103 mRestrictedProfileCreatePref.setVisible(false); 104 mRestrictedProfileDeletePref.setVisible(true); 105 106 AppRestrictionsFragment.prepareArgs(mRestrictedProfileAppsPref.getExtras(), 107 mRestrictedProfile.getUser().id, false, false); 108 } else if (UserManager.supportsMultipleUsers()) { 109 // Not in restricted profile, and it doesn't exist 110 mUnknownSourcesPref.setVisible(true); 111 112 mRestrictedProfileGroup.setVisible(true); 113 mRestrictedProfileEnterPref.setVisible(false); 114 mRestrictedProfileExitPref.setVisible(false); 115 mRestrictedProfileAppsPref.setVisible(false); 116 mRestrictedProfilePinPref.setVisible(false); 117 mRestrictedProfileCreatePref.setVisible(true); 118 mRestrictedProfileDeletePref.setVisible(false); 119 } else { 120 // Not in restricted profile, and can't create one either 121 mUnknownSourcesPref.setVisible(true); 122 123 mRestrictedProfileGroup.setVisible(false); 124 mRestrictedProfileEnterPref.setVisible(false); 125 mRestrictedProfileExitPref.setVisible(false); 126 mRestrictedProfileAppsPref.setVisible(false); 127 mRestrictedProfilePinPref.setVisible(false); 128 mRestrictedProfileCreatePref.setVisible(false); 129 mRestrictedProfileDeletePref.setVisible(false); 130 } 131 132 mRestrictedProfileCreatePref.setEnabled( 133 !isRestrictedProfileCreationInProgress()); 134 135 mUnknownSourcesPref.setEnabled(!isUnknownSourcesBlocked()); 136 137 mManageDeviceAdminPref.setVisible(hasActiveAdmins()); 138 mEnterprisePrivacyPref.setVisible(isDeviceManaged()); 139 } 140 isUnknownSourcesBlocked()141 private boolean isUnknownSourcesBlocked() { 142 final UserManager um = (UserManager) getContext().getSystemService(Context.USER_SERVICE); 143 return um.hasUserRestriction(UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES); 144 } 145 isDeviceManaged()146 private boolean isDeviceManaged() { 147 final DevicePolicyManager devicePolicyManager = getContext().getSystemService( 148 DevicePolicyManager.class); 149 return devicePolicyManager.isDeviceManaged(); 150 } 151 hasActiveAdmins()152 private boolean hasActiveAdmins() { 153 final DevicePolicyManager devicePolicyManager = getContext().getSystemService( 154 DevicePolicyManager.class); 155 final List<ComponentName> admins = devicePolicyManager.getActiveAdmins(); 156 return (admins != null && !admins.isEmpty()); 157 } 158 } 159