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 static android.app.admin.DevicePolicyResources.Strings.Settings.WORK_PROFILE_LOCATION_SWITCH_TITLE; 20 21 import android.app.settings.SettingsEnums; 22 import android.content.Context; 23 import android.database.ContentObserver; 24 import android.location.SettingInjectorService; 25 import android.os.Bundle; 26 import android.os.Handler; 27 import android.os.Looper; 28 import android.provider.Settings; 29 30 import androidx.preference.Preference; 31 import androidx.preference.PreferenceGroup; 32 33 import com.android.settings.R; 34 import com.android.settings.SettingsActivity; 35 import com.android.settings.dashboard.DashboardFragment; 36 import com.android.settings.search.BaseSearchIndexProvider; 37 import com.android.settings.widget.SettingsMainSwitchBar; 38 import com.android.settingslib.location.RecentLocationApps; 39 import com.android.settingslib.search.SearchIndexable; 40 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 primary 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 implements 65 LocationEnabler.LocationModeChangeListener { 66 67 private static final String TAG = "LocationSettings"; 68 private static final String RECENT_LOCATION_ACCESS_PREF_KEY = "recent_location_access"; 69 70 private LocationSwitchBarController mSwitchBarController; 71 private LocationEnabler mLocationEnabler; 72 private RecentLocationAccessPreferenceController mController; 73 private ContentObserver mContentObserver; 74 75 @Override getMetricsCategory()76 public int getMetricsCategory() { 77 return SettingsEnums.LOCATION; 78 } 79 80 @Override onActivityCreated(Bundle savedInstanceState)81 public void onActivityCreated(Bundle savedInstanceState) { 82 super.onActivityCreated(savedInstanceState); 83 final SettingsActivity activity = (SettingsActivity) getActivity(); 84 final SettingsMainSwitchBar switchBar = activity.getSwitchBar(); 85 switchBar.setTitle(getContext().getString(R.string.location_settings_primary_switch_title)); 86 switchBar.show(); 87 mSwitchBarController = new LocationSwitchBarController(activity, switchBar, 88 getSettingsLifecycle()); 89 mLocationEnabler = new LocationEnabler(getContext(), this, getSettingsLifecycle()); 90 mContentObserver = new ContentObserver(new Handler(Looper.getMainLooper())) { 91 @Override 92 public void onChange(boolean selfChange) { 93 mController.updateShowSystem(); 94 } 95 }; 96 getContentResolver().registerContentObserver( 97 Settings.Secure.getUriFor( 98 Settings.Secure.LOCATION_SHOW_SYSTEM_OPS), /* notifyForDescendants= */ 99 false, mContentObserver); 100 } 101 102 @Override onAttach(Context context)103 public void onAttach(Context context) { 104 super.onAttach(context); 105 106 use(AppLocationPermissionPreferenceController.class).init(this); 107 mController = use(RecentLocationAccessPreferenceController.class); 108 mController.init(this); 109 use(RecentLocationAccessSeeAllButtonPreferenceController.class).init(this); 110 use(LocationForWorkPreferenceController.class).init(this); 111 use(LocationSettingsFooterPreferenceController.class).init(this); 112 } 113 114 @Override onDestroy()115 public void onDestroy() { 116 super.onDestroy(); 117 getContentResolver().unregisterContentObserver(mContentObserver); 118 } 119 120 @Override getPreferenceScreenResId()121 protected int getPreferenceScreenResId() { 122 return R.xml.location_settings; 123 } 124 125 @Override onCreate(Bundle icicle)126 public void onCreate(Bundle icicle) { 127 super.onCreate(icicle); 128 129 replaceEnterpriseStringTitle("managed_profile_location_switch", 130 WORK_PROFILE_LOCATION_SWITCH_TITLE, R.string.managed_profile_location_switch_title); 131 } 132 133 @Override getLogTag()134 protected String getLogTag() { 135 return TAG; 136 } 137 138 @Override onLocationModeChanged(int mode, boolean restricted)139 public void onLocationModeChanged(int mode, boolean restricted) { 140 if (mLocationEnabler.isEnabled(mode)) { 141 scrollToPreference(RECENT_LOCATION_ACCESS_PREF_KEY); 142 } 143 } 144 addPreferencesSorted(List<Preference> prefs, PreferenceGroup container)145 static void addPreferencesSorted(List<Preference> prefs, PreferenceGroup container) { 146 // If there's some items to display, sort the items and add them to the container. 147 Collections.sort(prefs, 148 Comparator.comparing(lhs -> lhs.getTitle().toString())); 149 for (Preference entry : prefs) { 150 container.addPreference(entry); 151 } 152 } 153 154 @Override getHelpResource()155 public int getHelpResource() { 156 return R.string.help_url_location_access; 157 } 158 159 /** 160 * For Search. 161 */ 162 public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER = 163 new BaseSearchIndexProvider(R.xml.location_settings); 164 } 165