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.Activity; 20 import android.content.Context; 21 import android.os.Bundle; 22 import android.os.UserManager; 23 24 import androidx.annotation.Keep; 25 import androidx.preference.Preference; 26 27 import com.android.tv.settings.R; 28 import com.android.tv.settings.users.RestrictedProfileModel; 29 30 /** 31 * A subset of security settings to create a restricted profile. 32 */ 33 @Keep 34 public class CreateRestrictedProfileFragment extends BaseSecurityFragment { 35 private static final String KEY_RESTRICTED_PROFILE_ALREADY_CREATED = 36 "restricted_profile_already_created"; 37 38 private Preference mRestrictedProfileCreatePref; 39 private Preference mRestrictedProfileAlreadyCreatedPref; 40 newInstance()41 public static CreateRestrictedProfileFragment newInstance() { 42 return new CreateRestrictedProfileFragment(); 43 } 44 45 /** 46 * Called by other Fragments to decide whether to show or hide profile-related views. 47 */ isRestrictedProfileInEffect(Context context)48 public static boolean isRestrictedProfileInEffect(Context context) { 49 return new RestrictedProfileModel(context).isCurrentUser(); 50 } 51 52 @Override onCreatePreferences(Bundle savedInstanceState, String rootKey)53 public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { 54 setPreferencesFromResource(R.xml.create_restricted_profile, null); 55 mRestrictedProfileCreatePref = findPreference(KEY_RESTRICTED_PROFILE_CREATE); 56 mRestrictedProfileAlreadyCreatedPref = findPreference( 57 KEY_RESTRICTED_PROFILE_ALREADY_CREATED); 58 refresh(); 59 } 60 61 @Override refresh()62 protected void refresh() { 63 mRestrictedProfileCreatePref.setEnabled( 64 UserManager.supportsMultipleUsers() && !isRestrictedProfileCreationInProgress()); 65 if (mRestrictedProfile.getUser() != null) { 66 mRestrictedProfileAlreadyCreatedPref.setVisible(true); 67 mRestrictedProfileCreatePref.setVisible(false); 68 } else { 69 mRestrictedProfileAlreadyCreatedPref.setVisible(false); 70 mRestrictedProfileCreatePref.setVisible(true); 71 } 72 } 73 74 @Override onPreferenceTreeClick(Preference preference)75 public boolean onPreferenceTreeClick(Preference preference) { 76 final String key = preference.getKey(); 77 if (KEY_RESTRICTED_PROFILE_SKIP.equals(key)) { 78 requireActivity().setResult(Activity.RESULT_CANCELED); 79 requireActivity().finish(); 80 return true; 81 } 82 return super.onPreferenceTreeClick(preference); 83 } 84 85 @Override shouldExitAfterUpdatingApps()86 protected boolean shouldExitAfterUpdatingApps() { 87 return true; 88 } 89 } 90