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