1 /*
2  * Copyright (C) 2024 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.settings.location;
18 
19 import android.content.Context;
20 import android.os.UserHandle;
21 
22 import androidx.preference.Preference;
23 import androidx.preference.PreferenceScreen;
24 
25 import com.android.settings.Utils;
26 import com.android.settings.dashboard.profileselector.ProfileSelectFragment;
27 import com.android.settings.widget.RestrictedAppPreference;
28 
29 import java.util.List;
30 import java.util.Map;
31 
32 /**
33  * Retrieve the Location Services used in private profile user.
34  */
35 public class LocationInjectedServicesForPrivateProfilePreferenceController extends
36         LocationInjectedServiceBasePreferenceController {
LocationInjectedServicesForPrivateProfilePreferenceController( Context context, String key)37     public LocationInjectedServicesForPrivateProfilePreferenceController(
38             Context context, String key) {
39         super(context, key);
40     }
41 
42     @Override
injectLocationServices(PreferenceScreen screen)43     protected void injectLocationServices(PreferenceScreen screen) {
44         if (!android.os.Flags.allowPrivateProfile()
45                 || !android.multiuser.Flags.enablePrivateSpaceFeatures()
46                 || !android.multiuser.Flags.handleInterleavedSettingsForPrivateSpace()) {
47             return;
48         }
49         final UserHandle privateProfile = Utils.getProfileOfType(mUserManager,
50                 ProfileSelectFragment.ProfileType.PRIVATE);
51         if (privateProfile == null) {
52             return;
53         }
54         final Map<Integer, List<Preference>> prefs = getLocationServices();
55         for (Map.Entry<Integer, List<Preference>> entry : prefs.entrySet()) {
56             for (Preference pref : entry.getValue()) {
57                 if (pref instanceof RestrictedAppPreference) {
58                     ((RestrictedAppPreference) pref).checkRestrictionAndSetDisabled();
59                 }
60             }
61             if (entry.getKey() == privateProfile.getIdentifier()) {
62                 LocationSettings.addPreferencesSorted(entry.getValue(), screen);
63             }
64         }
65     }
66 }
67