OSDN Git Service

510c1625a58041773e1a340627b3998dcf5fc108
[android-x86/packages-apps-Settings.git] / src / com / android / settings / location / LocationSettings.java
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
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;
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 public class LocationSettings extends DashboardFragment {
64
65     private static final String TAG = "LocationSettings";
66
67     private LocationSwitchBarController mSwitchBarController;
68
69     @Override
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);
76     }
77
78
79     @Override
80     public int getMetricsCategory() {
81         return MetricsEvent.LOCATION;
82     }
83
84     @Override
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());
92     }
93
94     @Override
95     protected int getPreferenceScreenResId() {
96         return R.xml.location_settings;
97     }
98
99     @Override
100     protected String getLogTag() {
101         return TAG;
102     }
103
104     @Override
105     protected List<AbstractPreferenceController> getPreferenceControllers(Context context) {
106         return buildPreferenceControllers(context, this, getLifecycle());
107     }
108
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>() {
112             @Override
113             public int compare(Preference lhs, Preference rhs) {
114                 return lhs.getTitle().toString().compareTo(rhs.getTitle().toString());
115             }
116         });
117         for (Preference entry : prefs) {
118             container.addPreference(entry);
119         }
120     }
121
122     @Override
123     public int getHelpResource() {
124         return R.string.help_url_location_access;
125     }
126
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));
132         controllers.add(
133                 new RecentLocationRequestPreferenceController(context, fragment, lifecycle));
134         controllers.add(new LocationScanningPreferenceController(context));
135         controllers.add(
136                 new LocationServicePreferenceController(context, fragment, lifecycle));
137         controllers.add(new LocationFooterPreferenceController(context, lifecycle));
138         return controllers;
139     }
140
141     private static class SummaryProvider implements SummaryLoader.SummaryProvider {
142
143         private final Context mContext;
144         private final SummaryLoader mSummaryLoader;
145
146         public SummaryProvider(Context context, SummaryLoader summaryLoader) {
147             mContext = context;
148             mSummaryLoader = summaryLoader;
149         }
150
151         @Override
152         public void setListening(boolean listening) {
153             if (listening) {
154                 mSummaryLoader.setSummary(
155                     this, LocationPreferenceController.getLocationSummary(mContext));
156             }
157         }
158     }
159
160     public static final SummaryLoader.SummaryProviderFactory SUMMARY_PROVIDER_FACTORY
161             = new SummaryLoader.SummaryProviderFactory() {
162         @Override
163         public SummaryLoader.SummaryProvider createSummaryProvider(Activity activity,
164                                                                    SummaryLoader summaryLoader) {
165             return new SummaryProvider(activity, summaryLoader);
166         }
167     };
168
169     /**
170      * For Search.
171      */
172     public static final Indexable.SearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
173             new BaseSearchIndexProvider() {
174                 @Override
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);
180                 }
181
182                 @Override
183                 public List<AbstractPreferenceController> getPreferenceControllers(Context
184                         context) {
185                     return buildPreferenceControllers(context, null /* fragment */,
186                             null /* lifecycle */);
187                 }
188             };
189 }