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.os.SystemProperties; 29 import android.provider.Settings; 30 import android.util.Log; 31 32 import androidx.preference.Preference; 33 import androidx.preference.PreferenceGroup; 34 35 import com.android.settings.R; 36 import com.android.settings.SettingsActivity; 37 import com.android.settings.dashboard.DashboardFragment; 38 import com.android.settings.search.BaseSearchIndexProvider; 39 import com.android.settings.widget.SettingsMainSwitchBar; 40 import com.android.settingslib.location.RecentLocationApps; 41 import com.android.settingslib.search.SearchIndexable; 42 43 import java.util.Collections; 44 import java.util.Comparator; 45 import java.util.List; 46 47 /** 48 * System location settings (Settings > Location). The screen has three parts: 49 * <ul> 50 * <li>Platform location controls</li> 51 * <ul> 52 * <li>In switch bar: location primary switch. Used to toggle location on and off. 53 * </li> 54 * </ul> 55 * <li>Recent location requests: automatically populated by {@link RecentLocationApps}</li> 56 * <li>Location services: multi-app settings provided from outside the Android framework. Each 57 * is injected by a system-partition app via the {@link SettingInjectorService} API.</li> 58 * </ul> 59 * <p> 60 * Note that as of KitKat, the {@link SettingInjectorService} is the preferred method for OEMs to 61 * add their own settings to this page, rather than directly modifying the framework code. Among 62 * other things, this simplifies integration with future changes to the default (AOSP) 63 * implementation. 64 */ 65 @SearchIndexable 66 public class LocationSettings extends DashboardFragment implements 67 LocationEnabler.LocationModeChangeListener { 68 69 private static final String TAG = "LocationSettings"; 70 private static final String RECENT_LOCATION_ACCESS_PREF_KEY = "recent_location_access"; 71 72 private LocationSwitchBarController mSwitchBarController; 73 private LocationEnabler mLocationEnabler; 74 private RecentLocationAccessPreferenceController mController; 75 private ContentObserver mContentObserver; 76 77 /** 78 * Read-only boot property used to enable/disable geolocation toggle as part of privacy hub 79 * feature for chrome. 80 */ 81 private static final String RO_BOOT_ENABLE_PRIVACY_HUB_FOR_CHROME = 82 "ro.boot.enable_privacy_hub_for_chrome"; 83 84 @Override getMetricsCategory()85 public int getMetricsCategory() { 86 return SettingsEnums.LOCATION; 87 } 88 89 @Override onActivityCreated(Bundle savedInstanceState)90 public void onActivityCreated(Bundle savedInstanceState) { 91 super.onActivityCreated(savedInstanceState); 92 final SettingsActivity activity = (SettingsActivity) getActivity(); 93 final SettingsMainSwitchBar switchBar = activity.getSwitchBar(); 94 switchBar.setTitle(getContext().getString(R.string.location_settings_primary_switch_title)); 95 updateChromeSwitchBarPreference(switchBar); 96 switchBar.show(); 97 mSwitchBarController = new LocationSwitchBarController(activity, switchBar, 98 getSettingsLifecycle()); 99 mLocationEnabler = new LocationEnabler(getContext(), this, getSettingsLifecycle()); 100 mContentObserver = new ContentObserver(new Handler(Looper.getMainLooper())) { 101 @Override 102 public void onChange(boolean selfChange) { 103 mController.updateShowSystem(); 104 } 105 }; 106 getContentResolver().registerContentObserver( 107 Settings.Secure.getUriFor( 108 Settings.Secure.LOCATION_SHOW_SYSTEM_OPS), /* notifyForDescendants= */ 109 false, mContentObserver); 110 } 111 112 @Override onAttach(Context context)113 public void onAttach(Context context) { 114 super.onAttach(context); 115 116 use(AppLocationPermissionPreferenceController.class).init(this); 117 mController = use(RecentLocationAccessPreferenceController.class); 118 mController.init(this); 119 use(RecentLocationAccessSeeAllButtonPreferenceController.class).init(this); 120 use(LocationForWorkPreferenceController.class).init(this); 121 use(LocationSettingsFooterPreferenceController.class).init(this); 122 use(LocationForPrivateProfilePreferenceController.class).init(this); 123 } 124 125 @Override onDestroy()126 public void onDestroy() { 127 super.onDestroy(); 128 getContentResolver().unregisterContentObserver(mContentObserver); 129 } 130 131 @Override getPreferenceScreenResId()132 protected int getPreferenceScreenResId() { 133 return R.xml.location_settings; 134 } 135 136 @Override onCreate(Bundle icicle)137 public void onCreate(Bundle icicle) { 138 super.onCreate(icicle); 139 140 replaceEnterpriseStringTitle("managed_profile_location_switch", 141 WORK_PROFILE_LOCATION_SWITCH_TITLE, R.string.managed_profile_location_switch_title); 142 } 143 144 @Override getLogTag()145 protected String getLogTag() { 146 return TAG; 147 } 148 149 @Override onLocationModeChanged(int mode, boolean restricted)150 public void onLocationModeChanged(int mode, boolean restricted) { 151 if (mLocationEnabler.isEnabled(mode)) { 152 scrollToPreference(RECENT_LOCATION_ACCESS_PREF_KEY); 153 } 154 } 155 addPreferencesSorted(List<Preference> prefs, PreferenceGroup container)156 static void addPreferencesSorted(List<Preference> prefs, PreferenceGroup container) { 157 // If there's some items to display, sort the items and add them to the container. 158 Collections.sort(prefs, 159 Comparator.comparing(lhs -> lhs.getTitle().toString())); 160 for (Preference entry : prefs) { 161 container.addPreference(entry); 162 } 163 } 164 165 @Override getHelpResource()166 public int getHelpResource() { 167 return R.string.help_url_location_access; 168 } 169 170 /** 171 * For Search. 172 */ 173 public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER = 174 new BaseSearchIndexProvider(R.xml.location_settings); 175 176 /** 177 * Update switchbar config in case of Chrome devices and location is managed by chrome. 178 */ updateChromeSwitchBarPreference(final SettingsMainSwitchBar switchBar)179 private void updateChromeSwitchBarPreference(final SettingsMainSwitchBar switchBar) { 180 if (getContext().getResources().getBoolean(R.bool.config_disable_location_toggle_for_chrome) 181 && SystemProperties.getBoolean(RO_BOOT_ENABLE_PRIVACY_HUB_FOR_CHROME, false)) { 182 Log.i(TAG, "Disabling location toggle for chrome devices"); 183 switchBar.setClickable(false); 184 switchBar.setTooltipText(getResources().getString( 185 R.string.location_settings_tooltip_text_for_chrome)); 186 } 187 } 188 } 189