• 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.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.SettingsMainSwitchBar;
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 primary 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 implements
59         LocationEnabler.LocationModeChangeListener {
60 
61     private static final String TAG = "LocationSettings";
62     private static final String RECENT_LOCATION_ACCESS_PREF_KEY = "recent_location_access";
63 
64     private LocationSwitchBarController mSwitchBarController;
65     private LocationEnabler mLocationEnabler;
66 
67     @Override
getMetricsCategory()68     public int getMetricsCategory() {
69         return SettingsEnums.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 SettingsMainSwitchBar switchBar = activity.getSwitchBar();
77         switchBar.setTitle(getContext().getString(R.string.location_settings_primary_switch_title));
78         switchBar.show();
79         mSwitchBarController = new LocationSwitchBarController(activity, switchBar,
80                 getSettingsLifecycle());
81         mLocationEnabler = new LocationEnabler(getContext(), this, getSettingsLifecycle());
82     }
83 
84     @Override
onAttach(Context context)85     public void onAttach(Context context) {
86         super.onAttach(context);
87 
88         use(AppLocationPermissionPreferenceController.class).init(this);
89         use(RecentLocationAccessPreferenceController.class).init(this);
90         use(RecentLocationAccessSeeAllButtonPreferenceController.class).init(this);
91         use(LocationForWorkPreferenceController.class).init(this);
92         use(LocationSettingsFooterPreferenceController.class).init(this);
93     }
94 
95     @Override
getPreferenceScreenResId()96     protected int getPreferenceScreenResId() {
97         return R.xml.location_settings;
98     }
99 
100     @Override
getLogTag()101     protected String getLogTag() {
102         return TAG;
103     }
104 
105     @Override
onLocationModeChanged(int mode, boolean restricted)106     public void onLocationModeChanged(int mode, boolean restricted) {
107         if (mLocationEnabler.isEnabled(mode)) {
108             scrollToPreference(RECENT_LOCATION_ACCESS_PREF_KEY);
109         }
110     }
111 
addPreferencesSorted(List<Preference> prefs, PreferenceGroup container)112     static void addPreferencesSorted(List<Preference> prefs, PreferenceGroup container) {
113         // If there's some items to display, sort the items and add them to the container.
114         Collections.sort(prefs,
115                 Comparator.comparing(lhs -> lhs.getTitle().toString()));
116         for (Preference entry : prefs) {
117             container.addPreference(entry);
118         }
119     }
120 
121     @Override
getHelpResource()122     public int getHelpResource() {
123         return R.string.help_url_location_access;
124     }
125 
126     /**
127      * For Search.
128      */
129     public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
130             new BaseSearchIndexProvider(R.xml.location_settings);
131 }
132