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