2 * Copyright (C) 2011 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings.location;
19 import android.app.Activity;
20 import android.content.Context;
21 import android.location.SettingInjectorService;
22 import android.os.Bundle;
23 import android.provider.SearchIndexableResource;
24 import android.support.v7.preference.Preference;
25 import android.support.v7.preference.PreferenceGroup;
27 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
28 import com.android.settings.R;
29 import com.android.settings.SettingsActivity;
30 import com.android.settings.dashboard.DashboardFragment;
31 import com.android.settings.dashboard.SummaryLoader;
32 import com.android.settings.search.BaseSearchIndexProvider;
33 import com.android.settings.search.Indexable;
34 import com.android.settings.widget.SwitchBar;
35 import com.android.settingslib.core.AbstractPreferenceController;
36 import com.android.settingslib.core.lifecycle.Lifecycle;
37 import com.android.settingslib.location.RecentLocationApps;
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;
46 * System location settings (Settings > Location). The screen has three parts:
48 * <li>Platform location controls</li>
50 * <li>In switch bar: location master switch. Used to toggle location on and off.
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>
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)
63 public class LocationSettings extends DashboardFragment {
65 private static final String TAG = "LocationSettings";
67 private LocationSwitchBarController mSwitchBarController;
70 public void onCreate(Bundle icicle) {
71 super.onCreate(icicle);
72 final RecentLocationApps recentLocationApps = new RecentLocationApps(getActivity());
73 int locationRequestsApps = recentLocationApps.getAppList().size();
74 int locationRequestsPrefs = locationRequestsApps == 0 ? 1 : locationRequestsApps;
75 getPreferenceScreen().setInitialExpandedChildrenCount(locationRequestsPrefs + 2);
80 public int getMetricsCategory() {
81 return MetricsEvent.LOCATION;
85 public void onActivityCreated(Bundle savedInstanceState) {
86 super.onActivityCreated(savedInstanceState);
87 final SettingsActivity activity = (SettingsActivity) getActivity();
88 final SwitchBar switchBar = activity.getSwitchBar();
89 switchBar.setSwitchBarText(R.string.location_settings_master_switch_title,
90 R.string.location_settings_master_switch_title);
91 mSwitchBarController = new LocationSwitchBarController(activity, switchBar, getLifecycle());
95 protected int getPreferenceScreenResId() {
96 return R.xml.location_settings;
100 protected String getLogTag() {
105 protected List<AbstractPreferenceController> getPreferenceControllers(Context context) {
106 return buildPreferenceControllers(context, this, getLifecycle());
109 static void addPreferencesSorted(List<Preference> prefs, PreferenceGroup container) {
110 // If there's some items to display, sort the items and add them to the container.
111 Collections.sort(prefs, new Comparator<Preference>() {
113 public int compare(Preference lhs, Preference rhs) {
114 return lhs.getTitle().toString().compareTo(rhs.getTitle().toString());
117 for (Preference entry : prefs) {
118 container.addPreference(entry);
123 public int getHelpResource() {
124 return R.string.help_url_location_access;
127 private static List<AbstractPreferenceController> buildPreferenceControllers(
128 Context context, LocationSettings fragment, Lifecycle lifecycle) {
129 final List<AbstractPreferenceController> controllers = new ArrayList<>();
130 controllers.add(new AppLocationPermissionPreferenceController(context));
131 controllers.add(new LocationForWorkPreferenceController(context, lifecycle));
133 new RecentLocationRequestPreferenceController(context, fragment, lifecycle));
134 controllers.add(new LocationScanningPreferenceController(context));
136 new LocationServicePreferenceController(context, fragment, lifecycle));
137 controllers.add(new LocationFooterPreferenceController(context, lifecycle));
141 private static class SummaryProvider implements SummaryLoader.SummaryProvider {
143 private final Context mContext;
144 private final SummaryLoader mSummaryLoader;
146 public SummaryProvider(Context context, SummaryLoader summaryLoader) {
148 mSummaryLoader = summaryLoader;
152 public void setListening(boolean listening) {
154 mSummaryLoader.setSummary(
155 this, LocationPreferenceController.getLocationSummary(mContext));
160 public static final SummaryLoader.SummaryProviderFactory SUMMARY_PROVIDER_FACTORY
161 = new SummaryLoader.SummaryProviderFactory() {
163 public SummaryLoader.SummaryProvider createSummaryProvider(Activity activity,
164 SummaryLoader summaryLoader) {
165 return new SummaryProvider(activity, summaryLoader);
172 public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
173 new BaseSearchIndexProvider() {
175 public List<SearchIndexableResource> getXmlResourcesToIndex(
176 Context context, boolean enabled) {
177 final SearchIndexableResource sir = new SearchIndexableResource(context);
178 sir.xmlResId = R.xml.location_settings;
179 return Arrays.asList(sir);
183 public List<AbstractPreferenceController> getPreferenceControllers(Context
185 return buildPreferenceControllers(context, null /* fragment */,
186 null /* lifecycle */);