1 /*
2  * Copyright (C) 2011 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.app.settings.SettingsEnums;
20 import android.content.Context;
21 import android.location.SettingInjectorService;
22 import android.os.Bundle;
23 
24 import androidx.preference.Preference;
25 import androidx.preference.PreferenceGroup;
26 
27 import com.android.settings.R;
28 import com.android.settings.SettingsActivity;
29 import com.android.settings.dashboard.DashboardFragment;
30 import com.android.settings.search.BaseSearchIndexProvider;
31 import com.android.settings.widget.SwitchBar;
32 import com.android.settingslib.location.RecentLocationApps;
33 import com.android.settingslib.search.SearchIndexable;
34 
35 import java.util.Collections;
36 import java.util.Comparator;
37 import java.util.List;
38 
39 /**
40  * System location settings (Settings > Location). The screen has three parts:
41  * <ul>
42  *     <li>Platform location controls</li>
43  *     <ul>
44  *         <li>In switch bar: location master switch. Used to toggle location on and off.
45  *         </li>
46  *     </ul>
47  *     <li>Recent location requests: automatically populated by {@link RecentLocationApps}</li>
48  *     <li>Location services: multi-app settings provided from outside the Android framework. Each
49  *     is injected by a system-partition app via the {@link SettingInjectorService} API.</li>
50  * </ul>
51  * <p>
52  * Note that as of KitKat, the {@link SettingInjectorService} is the preferred method for OEMs to
53  * add their own settings to this page, rather than directly modifying the framework code. Among
54  * other things, this simplifies integration with future changes to the default (AOSP)
55  * implementation.
56  */
57 @SearchIndexable
58 public class LocationSettings extends DashboardFragment {
59 
60     private static final String TAG = "LocationSettings";
61 
62     private LocationSwitchBarController mSwitchBarController;
63 
64     @Override
getMetricsCategory()65     public int getMetricsCategory() {
66         return SettingsEnums.LOCATION;
67     }
68 
69     @Override
onActivityCreated(Bundle savedInstanceState)70     public void onActivityCreated(Bundle savedInstanceState) {
71         super.onActivityCreated(savedInstanceState);
72         final SettingsActivity activity = (SettingsActivity) getActivity();
73         final SwitchBar switchBar = activity.getSwitchBar();
74         switchBar.setSwitchBarText(R.string.location_settings_master_switch_title,
75                 R.string.location_settings_master_switch_title);
76         mSwitchBarController = new LocationSwitchBarController(activity, switchBar,
77                 getSettingsLifecycle());
78         switchBar.show();
79     }
80 
81     @Override
onAttach(Context context)82     public void onAttach(Context context) {
83         super.onAttach(context);
84 
85         use(AppLocationPermissionPreferenceController.class).init(this);
86         use(RecentLocationRequestPreferenceController.class).init(this);
87         use(LocationServicePreferenceController.class).init(this);
88         use(LocationFooterPreferenceController.class).init(this);
89         use(LocationForWorkPreferenceController.class).init(this);
90         use(LocationServiceForWorkPreferenceController.class).init(this);
91     }
92 
93     @Override
getPreferenceScreenResId()94     protected int getPreferenceScreenResId() {
95         return R.xml.location_settings;
96     }
97 
98     @Override
getLogTag()99     protected String getLogTag() {
100         return TAG;
101     }
102 
addPreferencesSorted(List<Preference> prefs, PreferenceGroup container)103     static void addPreferencesSorted(List<Preference> prefs, PreferenceGroup container) {
104         // If there's some items to display, sort the items and add them to the container.
105         Collections.sort(prefs,
106                 Comparator.comparing(lhs -> lhs.getTitle().toString()));
107         for (Preference entry : prefs) {
108             container.addPreference(entry);
109         }
110     }
111 
112     @Override
getHelpResource()113     public int getHelpResource() {
114         return R.string.help_url_location_access;
115     }
116 
117     /**
118      * For Search.
119      */
120     public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
121             new BaseSearchIndexProvider(R.xml.location_settings);
122 }
123