• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.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;
26 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
27 import com.android.settings.R;
28 import com.android.settings.SettingsActivity;
29 import com.android.settings.dashboard.DashboardFragment;
30 import com.android.settings.dashboard.SummaryLoader;
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 java.util.ArrayList;
38 import java.util.Arrays;
39 import java.util.Collections;
40 import java.util.Comparator;
41 import java.util.List;
42 
43 /**
44  * System location settings (Settings > Location). The screen has three parts:
45  * <ul>
46  *     <li>Platform location controls</li>
47  *     <ul>
48  *         <li>In switch bar: location master switch. Used to toggle location on and off.
49  *         </li>
50  *     </ul>
51  *     <li>Recent location requests: automatically populated by {@link RecentLocationApps}</li>
52  *     <li>Location services: multi-app settings provided from outside the Android framework. Each
53  *     is injected by a system-partition app via the {@link SettingInjectorService} API.</li>
54  * </ul>
55  * <p>
56  * Note that as of KitKat, the {@link SettingInjectorService} is the preferred method for OEMs to
57  * add their own settings to this page, rather than directly modifying the framework code. Among
58  * other things, this simplifies integration with future changes to the default (AOSP)
59  * implementation.
60  */
61 public class LocationSettings extends DashboardFragment {
62 
63     private static final String TAG = "LocationSettings";
64 
65     private LocationSwitchBarController mSwitchBarController;
66 
67     @Override
getMetricsCategory()68     public int getMetricsCategory() {
69         return MetricsEvent.LOCATION;
70     }
71 
72     @Override
onActivityCreated(Bundle savedInstanceState)73     public void onActivityCreated(Bundle savedInstanceState) {
74         super.onActivityCreated(savedInstanceState);
75         final SettingsActivity activity = (SettingsActivity) getActivity();
76         final SwitchBar switchBar = activity.getSwitchBar();
77         switchBar.setSwitchBarText(R.string.location_settings_master_switch_title,
78                 R.string.location_settings_master_switch_title);
79         mSwitchBarController = new LocationSwitchBarController(activity, switchBar, getLifecycle());
80         switchBar.show();
81     }
82 
83     @Override
getPreferenceScreenResId()84     protected int getPreferenceScreenResId() {
85         return R.xml.location_settings;
86     }
87 
88     @Override
getLogTag()89     protected String getLogTag() {
90         return TAG;
91     }
92 
93     @Override
createPreferenceControllers(Context context)94     protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
95         return buildPreferenceControllers(context, this, getLifecycle());
96     }
97 
addPreferencesSorted(List<Preference> prefs, PreferenceGroup container)98     static void addPreferencesSorted(List<Preference> prefs, PreferenceGroup container) {
99         // If there's some items to display, sort the items and add them to the container.
100         Collections.sort(prefs, new Comparator<Preference>() {
101             @Override
102             public int compare(Preference lhs, Preference rhs) {
103                 return lhs.getTitle().toString().compareTo(rhs.getTitle().toString());
104             }
105         });
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));
120         controllers.add(new LocationForWorkPreferenceController(context, lifecycle));
121         controllers.add(
122                 new RecentLocationRequestPreferenceController(context, fragment, lifecycle));
123         controllers.add(new LocationScanningPreferenceController(context));
124         controllers.add(
125                 new LocationServicePreferenceController(context, fragment, lifecycle));
126         controllers.add(new LocationFooterPreferenceController(context, lifecycle));
127         return controllers;
128     }
129 
130     private static class SummaryProvider implements SummaryLoader.SummaryProvider {
131 
132         private final Context mContext;
133         private final SummaryLoader mSummaryLoader;
134 
SummaryProvider(Context context, SummaryLoader summaryLoader)135         public SummaryProvider(Context context, SummaryLoader summaryLoader) {
136             mContext = context;
137             mSummaryLoader = summaryLoader;
138         }
139 
140         @Override
setListening(boolean listening)141         public void setListening(boolean listening) {
142             if (listening) {
143                 mSummaryLoader.setSummary(
144                     this, LocationPreferenceController.getLocationSummary(mContext));
145             }
146         }
147     }
148 
149     public static final SummaryLoader.SummaryProviderFactory SUMMARY_PROVIDER_FACTORY
150             = new SummaryLoader.SummaryProviderFactory() {
151         @Override
152         public SummaryLoader.SummaryProvider createSummaryProvider(Activity activity,
153                                                                    SummaryLoader summaryLoader) {
154             return new SummaryProvider(activity, summaryLoader);
155         }
156     };
157 
158     /**
159      * For Search.
160      */
161     public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
162             new BaseSearchIndexProvider() {
163                 @Override
164                 public List<SearchIndexableResource> getXmlResourcesToIndex(
165                         Context context, boolean enabled) {
166                     final SearchIndexableResource sir = new SearchIndexableResource(context);
167                     sir.xmlResId = R.xml.location_settings;
168                     return Arrays.asList(sir);
169                 }
170 
171                 @Override
172                 public List<AbstractPreferenceController> createPreferenceControllers(Context
173                         context) {
174                     return buildPreferenceControllers(context, null /* fragment */,
175                             null /* lifecycle */);
176                 }
177             };
178 }
179