Refactor LocationSettings

- Extends BasePreferenceController in LocationBasePreferenceController
which binds preference key based on xml file instead of writing the key
in java code. Then the controller can be used in many xmls.
- Modify LocationServicePreferenceController to support only personal or
profile user.

Bug: 141601408
Test: manual, robolectric
Change-Id: I51ee950dfb87474df84a8dc3db55fb911edcf599
This commit is contained in:
Raff Tsai
2019-11-22 11:35:40 +08:00
parent a6198c25be
commit 2229585e88
17 changed files with 209 additions and 273 deletions

View File

@@ -10,9 +10,13 @@
See the License for the specific language governing permissions and See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
--> -->
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" <PreferenceScreen
android:title="@string/location_category_recent_location_requests" xmlns:android="http://schemas.android.com/apk/res/android"
android:key="recent_location_requests_see_all"> xmlns:settings="http://schemas.android.com/apk/res-auto"
<PreferenceCategory android:title="@string/location_category_recent_location_requests"
android:key="all_recent_location_requests"/> android:key="recent_location_requests_see_all">
<PreferenceCategory
android:key="all_recent_location_requests"
settings:controller="com.android.settings.location.RecentLocationRequestSeeAllPreferenceController"/>
</PreferenceScreen> </PreferenceScreen>

View File

@@ -14,64 +14,71 @@
limitations under the License. limitations under the License.
--> -->
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" <PreferenceScreen
xmlns:settings="http://schemas.android.com/apk/res-auto" xmlns:android="http://schemas.android.com/apk/res/android"
android:key="location_settings" xmlns:settings="http://schemas.android.com/apk/res-auto"
android:title="@string/location_settings_title" android:key="location_settings"
settings:keywords="@string/keywords_location"> android:title="@string/location_settings_title"
settings:keywords="@string/keywords_location">
<PreferenceCategory <PreferenceCategory
android:key="recent_location_requests" android:key="recent_location_requests"
android:title="@string/location_category_recent_location_requests"/> android:title="@string/location_category_recent_location_requests"
settings:controller="com.android.settings.location.RecentLocationRequestPreferenceController"/>
<Preference
android:key="recent_location_requests_see_all_button"
android:title="@string/location_recent_location_requests_see_all"
android:icon="@drawable/ic_chevron_right_24dp"
android:fragment="com.android.settings.location.RecentLocationRequestSeeAllFragment"
settings:searchable="false"/>
<PreferenceCategory
android:key="location_advanced_settings"
android:layout="@layout/preference_category_no_label"
settings:initialExpandedChildrenCount="2">
<!-- This preference category gets removed if new_recent_location_ui is disabled -->
<Preference
android:key="app_level_permissions"
android:title="@string/location_app_level_permissions"
settings:controller="com.android.settings.location.AppLocationPermissionPreferenceController">
<intent android:action="android.intent.action.MANAGE_PERMISSION_APPS">
<extra android:name="android.intent.extra.PERMISSION_NAME"
android:value="android.permission-group.LOCATION"/>
</intent>
</Preference>
<Preference <Preference
android:key="recent_location_requests_see_all_button" android:fragment="com.android.settings.location.ScanningSettings"
android:title="@string/location_recent_location_requests_see_all" android:key="location_scanning"
android:icon="@drawable/ic_chevron_right_24dp" android:title="@string/location_scanning_screen_title"
android:fragment="com.android.settings.location.RecentLocationRequestSeeAllFragment" settings:controller="com.android.settings.location.LocationScanningPreferenceController"/>
settings:searchable="false"/>
<!-- This preference gets removed if there is no managed profile -->
<com.android.settingslib.RestrictedSwitchPreference
android:enabled="false"
android:key="managed_profile_location_switch"
android:selectable="true"
android:title="@string/managed_profile_location_switch_title"
settings:controller="com.android.settings.location.LocationForWorkPreferenceController"
settings:useAdminDisabledSummary="true"/>
<PreferenceCategory <PreferenceCategory
android:key="location_advanced_settings" android:key="location_services"
android:layout="@layout/preference_category_no_label" android:layout="@layout/preference_category_no_label"
settings:initialExpandedChildrenCount="2"> settings:controller="com.android.settings.location.LocationServicePreferenceController"/>
<!-- This preference category gets removed if new_recent_location_ui is disabled -->
<Preference
android:key="app_level_permissions"
android:title="@string/location_app_level_permissions">
<intent android:action="android.intent.action.MANAGE_PERMISSION_APPS">
<extra android:name="android.intent.extra.PERMISSION_NAME"
android:value="android.permission-group.LOCATION" />
</intent>
</Preference>
<Preference
android:key="location_scanning"
android:title="@string/location_scanning_screen_title"
android:fragment="com.android.settings.location.ScanningSettings"/>
<!-- This preference gets removed if there is no managed profile -->
<com.android.settingslib.RestrictedSwitchPreference
android:key="managed_profile_location_switch"
android:title="@string/managed_profile_location_switch_title"
settings:useAdminDisabledSummary="true"
android:enabled="false"
android:selectable="true" />
<PreferenceCategory
android:key="location_services"
android:layout="@layout/preference_category_no_label"/>
<!-- This preference gets removed if there is no managed profile -->
<PreferenceCategory
android:title="@string/managed_profile_location_services"
android:key="location_services_managed_profile" />
</PreferenceCategory>
<!-- This preference gets removed if there is no managed profile -->
<PreferenceCategory <PreferenceCategory
android:key="location_footer" android:key="location_services_managed_profile"
android:layout="@layout/preference_category_no_label" android:title="@string/managed_profile_location_services"/>
settings:allowDividerAbove="false"/>
</PreferenceCategory>
<PreferenceCategory
android:key="location_footer"
android:layout="@layout/preference_category_no_label"
settings:allowDividerAbove="false"
settings:controller="com.android.settings.location.LocationFooterPreferenceController"/>
</PreferenceScreen> </PreferenceScreen>

View File

@@ -16,7 +16,6 @@ import androidx.preference.Preference;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.Utils; import com.android.settings.Utils;
import com.android.settings.core.PreferenceControllerMixin; import com.android.settings.core.PreferenceControllerMixin;
import com.android.settingslib.core.lifecycle.Lifecycle;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
@@ -25,7 +24,6 @@ import java.util.concurrent.atomic.AtomicInteger;
public class AppLocationPermissionPreferenceController extends public class AppLocationPermissionPreferenceController extends
LocationBasePreferenceController implements PreferenceControllerMixin { LocationBasePreferenceController implements PreferenceControllerMixin {
private static final String KEY_APP_LEVEL_PERMISSIONS = "app_level_permissions";
/** Total number of apps that has location permission. */ /** Total number of apps that has location permission. */
@VisibleForTesting @VisibleForTesting
int mNumTotal = -1; int mNumTotal = -1;
@@ -40,20 +38,16 @@ public class AppLocationPermissionPreferenceController extends
private final LocationManager mLocationManager; private final LocationManager mLocationManager;
private Preference mPreference; private Preference mPreference;
public AppLocationPermissionPreferenceController(Context context, Lifecycle lifecycle) { public AppLocationPermissionPreferenceController(Context context, String key) {
super(context, lifecycle); super(context, key);
mLocationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); mLocationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
} }
@Override @Override
public String getPreferenceKey() { public int getAvailabilityStatus() {
return KEY_APP_LEVEL_PERMISSIONS;
}
@Override
public boolean isAvailable() {
return Settings.Global.getInt(mContext.getContentResolver(), return Settings.Global.getInt(mContext.getContentResolver(),
Settings.Global.LOCATION_SETTINGS_LINK_TO_PERMISSIONS_ENABLED, 1) == 1; Settings.Global.LOCATION_SETTINGS_LINK_TO_PERMISSIONS_ENABLED, 1) == 1 ? AVAILABLE
: UNSUPPORTED_ON_DEVICE;
} }
@Override @Override

View File

@@ -16,29 +16,46 @@ package com.android.settings.location;
import android.content.Context; import android.content.Context;
import android.os.UserManager; import android.os.UserManager;
import com.android.settings.core.PreferenceControllerMixin; import com.android.settings.core.BasePreferenceController;
import com.android.settingslib.core.AbstractPreferenceController; import com.android.settings.dashboard.DashboardFragment;
import com.android.settingslib.core.lifecycle.Lifecycle; import com.android.settingslib.core.lifecycle.Lifecycle;
/** /**
* A base controller for preferences that listens to location settings change and modifies location * A base controller for preferences that listens to location settings change and modifies location
* settings. * settings.
*/ */
public abstract class LocationBasePreferenceController extends AbstractPreferenceController public abstract class LocationBasePreferenceController extends BasePreferenceController
implements PreferenceControllerMixin, LocationEnabler.LocationModeChangeListener { implements LocationEnabler.LocationModeChangeListener {
protected final UserManager mUserManager; protected UserManager mUserManager;
protected final LocationEnabler mLocationEnabler; protected LocationEnabler mLocationEnabler;
protected DashboardFragment mFragment;
protected Lifecycle mLifecycle;
public LocationBasePreferenceController(Context context, Lifecycle lifecycle) { /**
super(context); * Constructor of LocationBasePreferenceController. {@link BasePreferenceController} uses
mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE); * reflection to create controller, all controllers extends {@link BasePreferenceController}
mLocationEnabler = new LocationEnabler(context, this /* listener */, lifecycle); * should have this function.
*/
public LocationBasePreferenceController(Context context, String key) {
super(context, key);
}
/**
* Initialize {@link LocationEnabler} in this controller
*
* @param fragment The {@link DashboardFragment} uses the controller.
*/
public void init(DashboardFragment fragment) {
mFragment = fragment;
mUserManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
mLifecycle = mFragment.getSettingsLifecycle();
mLocationEnabler = new LocationEnabler(mContext, this /* listener */, mLifecycle);
} }
@Override @Override
public boolean isAvailable() { public int getAvailabilityStatus() {
return true; return AVAILABLE;
} }
} }

View File

@@ -13,7 +13,6 @@
*/ */
package com.android.settings.location; package com.android.settings.location;
import android.content.ComponentName;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.pm.ActivityInfo; import android.content.pm.ActivityInfo;
@@ -24,7 +23,6 @@ import android.content.pm.ResolveInfo;
import android.location.LocationManager; import android.location.LocationManager;
import android.util.Log; import android.util.Log;
import androidx.annotation.VisibleForTesting;
import androidx.preference.Preference; import androidx.preference.Preference;
import androidx.preference.PreferenceCategory; import androidx.preference.PreferenceCategory;
@@ -41,23 +39,16 @@ import java.util.List;
public class LocationFooterPreferenceController extends LocationBasePreferenceController { public class LocationFooterPreferenceController extends LocationBasePreferenceController {
private static final String TAG = "LocationFooter"; private static final String TAG = "LocationFooter";
private static final String KEY_LOCATION_FOOTER = "location_footer";
private static final Intent INJECT_INTENT = private static final Intent INJECT_INTENT =
new Intent(LocationManager.SETTINGS_FOOTER_DISPLAYED_ACTION); new Intent(LocationManager.SETTINGS_FOOTER_DISPLAYED_ACTION);
private final PackageManager mPackageManager; private final PackageManager mPackageManager;
public LocationFooterPreferenceController(Context context) { public LocationFooterPreferenceController(Context context, String key) {
// we don't care location mode changes, so pass in a null lifecycle to disable listening super(context, key);
super(context, null);
mPackageManager = context.getPackageManager(); mPackageManager = context.getPackageManager();
} }
@Override
public String getPreferenceKey() {
return KEY_LOCATION_FOOTER;
}
/** /**
* Insert footer preferences. * Insert footer preferences.
*/ */
@@ -97,8 +88,8 @@ public class LocationFooterPreferenceController extends LocationBasePreferenceCo
* inject. * inject.
*/ */
@Override @Override
public boolean isAvailable() { public int getAvailabilityStatus() {
return !getFooterData().isEmpty(); return !getFooterData().isEmpty() ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
} }
/** /**

View File

@@ -17,6 +17,7 @@ package com.android.settings.location;
import android.content.Context; import android.content.Context;
import android.os.UserManager; import android.os.UserManager;
import android.text.TextUtils;
import androidx.preference.Preference; import androidx.preference.Preference;
import androidx.preference.PreferenceScreen; import androidx.preference.PreferenceScreen;
@@ -25,25 +26,18 @@ import com.android.settings.R;
import com.android.settings.Utils; import com.android.settings.Utils;
import com.android.settingslib.RestrictedLockUtils; import com.android.settingslib.RestrictedLockUtils;
import com.android.settingslib.RestrictedSwitchPreference; import com.android.settingslib.RestrictedSwitchPreference;
import com.android.settingslib.core.lifecycle.Lifecycle;
public class LocationForWorkPreferenceController extends LocationBasePreferenceController { public class LocationForWorkPreferenceController extends LocationBasePreferenceController {
/**
* Key for managed profile location switch preference. Shown only
* if there is a managed profile.
*/
private static final String KEY_MANAGED_PROFILE_SWITCH = "managed_profile_location_switch";
private RestrictedSwitchPreference mPreference; private RestrictedSwitchPreference mPreference;
public LocationForWorkPreferenceController(Context context, Lifecycle lifecycle) { public LocationForWorkPreferenceController(Context context, String key) {
super(context, lifecycle); super(context, key);
} }
@Override @Override
public boolean handlePreferenceTreeClick(Preference preference) { public boolean handlePreferenceTreeClick(Preference preference) {
if (KEY_MANAGED_PROFILE_SWITCH.equals(preference.getKey())) { if (TextUtils.equals(preference.getKey(), getPreferenceKey())) {
final boolean switchState = mPreference.isChecked(); final boolean switchState = mPreference.isChecked();
mUserManager.setUserRestriction(UserManager.DISALLOW_SHARE_LOCATION, !switchState, mUserManager.setUserRestriction(UserManager.DISALLOW_SHARE_LOCATION, !switchState,
Utils.getManagedProfile(mUserManager)); Utils.getManagedProfile(mUserManager));
@@ -57,19 +51,14 @@ public class LocationForWorkPreferenceController extends LocationBasePreferenceC
@Override @Override
public void displayPreference(PreferenceScreen screen) { public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen); super.displayPreference(screen);
mPreference = screen.findPreference(KEY_MANAGED_PROFILE_SWITCH); mPreference = screen.findPreference(getPreferenceKey());
} }
@Override @Override
public boolean isAvailable() { public int getAvailabilityStatus() {
// Looking for a managed profile. If there are no managed profiles then we are removing the // Looking for a managed profile. If there are no managed profiles then we are removing the
// managed profile category. // managed profile category.
return Utils.getManagedProfile(mUserManager) != null; return Utils.getManagedProfile(mUserManager) != null ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
}
@Override
public String getPreferenceKey() {
return KEY_MANAGED_PROFILE_SWITCH;
} }
@Override @Override

View File

@@ -19,19 +19,14 @@ package com.android.settings.location;
import android.content.Context; import android.content.Context;
import android.provider.Settings; import android.provider.Settings;
import androidx.annotation.VisibleForTesting;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.core.BasePreferenceController; import com.android.settings.core.BasePreferenceController;
public class LocationScanningPreferenceController extends BasePreferenceController { public class LocationScanningPreferenceController extends BasePreferenceController {
@VisibleForTesting static final String KEY_LOCATION_SCANNING = "location_scanning";
private final Context mContext;
public LocationScanningPreferenceController(Context context) { public LocationScanningPreferenceController(Context context, String key) {
super(context, KEY_LOCATION_SCANNING); super(context, key);
mContext = context;
} }
@Override @Override

View File

@@ -27,8 +27,8 @@ import androidx.preference.PreferenceCategory;
import androidx.preference.PreferenceScreen; import androidx.preference.PreferenceScreen;
import com.android.settings.Utils; import com.android.settings.Utils;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.widget.RestrictedAppPreference; import com.android.settings.widget.RestrictedAppPreference;
import com.android.settingslib.core.lifecycle.Lifecycle;
import com.android.settingslib.core.lifecycle.LifecycleObserver; import com.android.settingslib.core.lifecycle.LifecycleObserver;
import com.android.settingslib.core.lifecycle.events.OnPause; import com.android.settingslib.core.lifecycle.events.OnPause;
import com.android.settingslib.core.lifecycle.events.OnResume; import com.android.settingslib.core.lifecycle.events.OnResume;
@@ -41,40 +41,31 @@ public class LocationServicePreferenceController extends LocationBasePreferenceC
private static final String TAG = "LocationServicePrefCtrl"; private static final String TAG = "LocationServicePrefCtrl";
/** Key for preference category "Location services" */ /** Key for preference category "Location services" */
private static final String KEY_LOCATION_SERVICES = "location_services"; @VisibleForTesting
static final String KEY_LOCATION_SERVICES = "location_services";
/** Key for preference category "Location services for work" */ /** Key for preference category "Location services for work" */
private static final String KEY_LOCATION_SERVICES_MANAGED = "location_services_managed_profile"; @VisibleForTesting
static final String KEY_LOCATION_SERVICES_MANAGED = "location_services_managed_profile";
@VisibleForTesting @VisibleForTesting
static final IntentFilter INTENT_FILTER_INJECTED_SETTING_CHANGED = static final IntentFilter INTENT_FILTER_INJECTED_SETTING_CHANGED =
new IntentFilter(SettingInjectorService.ACTION_INJECTED_SETTING_CHANGED); new IntentFilter(SettingInjectorService.ACTION_INJECTED_SETTING_CHANGED);
private PreferenceCategory mCategoryLocationServices; private PreferenceCategory mCategoryLocationServices;
private PreferenceCategory mCategoryLocationServicesManaged; private PreferenceCategory mCategoryLocationServicesManaged;
private final LocationSettings mFragment; @VisibleForTesting
private final AppSettingsInjector mInjector; AppSettingsInjector mInjector;
/** Receives UPDATE_INTENT */ /** Receives UPDATE_INTENT */
@VisibleForTesting @VisibleForTesting
BroadcastReceiver mInjectedSettingsReceiver; BroadcastReceiver mInjectedSettingsReceiver;
public LocationServicePreferenceController(Context context, LocationSettings fragment, public LocationServicePreferenceController(Context context, String key) {
Lifecycle lifecycle) { super(context, key);
this(context, fragment, lifecycle, new AppSettingsInjector(context));
}
@VisibleForTesting
LocationServicePreferenceController(Context context, LocationSettings fragment,
Lifecycle lifecycle, AppSettingsInjector injector) {
super(context, lifecycle);
mFragment = fragment;
mInjector = injector;
if (lifecycle != null) {
lifecycle.addObserver(this);
}
} }
@Override @Override
public String getPreferenceKey() { public void init(DashboardFragment fragment) {
return KEY_LOCATION_SERVICES; super.init(fragment);
mInjector = new AppSettingsInjector(mContext);
} }
@Override @Override
@@ -86,8 +77,12 @@ public class LocationServicePreferenceController extends LocationBasePreferenceC
@Override @Override
public void updateState(Preference preference) { public void updateState(Preference preference) {
mCategoryLocationServices.removeAll(); if (mCategoryLocationServices != null) {
mCategoryLocationServicesManaged.removeAll(); mCategoryLocationServices.removeAll();
}
if (mCategoryLocationServicesManaged != null) {
mCategoryLocationServicesManaged.removeAll();
}
final Map<Integer, List<Preference>> prefs = getLocationServices(); final Map<Integer, List<Preference>> prefs = getLocationServices();
boolean showPrimary = false; boolean showPrimary = false;
boolean showManaged = false; boolean showManaged = false;
@@ -98,16 +93,25 @@ public class LocationServicePreferenceController extends LocationBasePreferenceC
} }
} }
if (entry.getKey() == UserHandle.myUserId()) { if (entry.getKey() == UserHandle.myUserId()) {
LocationSettings.addPreferencesSorted(entry.getValue(), mCategoryLocationServices); if (mCategoryLocationServices != null) {
LocationSettings.addPreferencesSorted(entry.getValue(),
mCategoryLocationServices);
}
showPrimary = true; showPrimary = true;
} else { } else {
LocationSettings.addPreferencesSorted(entry.getValue(), if (mCategoryLocationServicesManaged != null) {
mCategoryLocationServicesManaged); LocationSettings.addPreferencesSorted(entry.getValue(),
mCategoryLocationServicesManaged);
}
showManaged = true; showManaged = true;
} }
} }
mCategoryLocationServices.setVisible(showPrimary); if (mCategoryLocationServices != null) {
mCategoryLocationServicesManaged.setVisible(showManaged); mCategoryLocationServices.setVisible(showPrimary);
}
if (mCategoryLocationServicesManaged != null) {
mCategoryLocationServicesManaged.setVisible(showManaged);
}
} }
@Override @Override

View File

@@ -29,12 +29,9 @@ import com.android.settings.SettingsActivity;
import com.android.settings.dashboard.DashboardFragment; import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.search.BaseSearchIndexProvider; import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settings.widget.SwitchBar; import com.android.settings.widget.SwitchBar;
import com.android.settingslib.core.AbstractPreferenceController;
import com.android.settingslib.core.lifecycle.Lifecycle;
import com.android.settingslib.location.RecentLocationApps; import com.android.settingslib.location.RecentLocationApps;
import com.android.settingslib.search.SearchIndexable; import com.android.settingslib.search.SearchIndexable;
import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
import java.util.List; import java.util.List;
@@ -81,6 +78,17 @@ public class LocationSettings extends DashboardFragment {
switchBar.show(); switchBar.show();
} }
@Override
public void onAttach(Context context) {
super.onAttach(context);
use(AppLocationPermissionPreferenceController.class).init(this);
use(RecentLocationRequestPreferenceController.class).init(this);
use(LocationServicePreferenceController.class).init(this);
use(LocationFooterPreferenceController.class).init(this);
use(LocationForWorkPreferenceController.class).init(this);
}
@Override @Override
protected int getPreferenceScreenResId() { protected int getPreferenceScreenResId() {
return R.xml.location_settings; return R.xml.location_settings;
@@ -91,11 +99,6 @@ public class LocationSettings extends DashboardFragment {
return TAG; return TAG;
} }
@Override
protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
return buildPreferenceControllers(context, this, getSettingsLifecycle());
}
static void addPreferencesSorted(List<Preference> prefs, PreferenceGroup container) { static void addPreferencesSorted(List<Preference> prefs, PreferenceGroup container) {
// If there's some items to display, sort the items and add them to the container. // If there's some items to display, sort the items and add them to the container.
Collections.sort(prefs, Collections.sort(prefs,
@@ -110,29 +113,9 @@ public class LocationSettings extends DashboardFragment {
return R.string.help_url_location_access; return R.string.help_url_location_access;
} }
private static List<AbstractPreferenceController> buildPreferenceControllers(
Context context, LocationSettings fragment, Lifecycle lifecycle) {
final List<AbstractPreferenceController> controllers = new ArrayList<>();
controllers.add(new AppLocationPermissionPreferenceController(context, lifecycle));
controllers.add(new LocationForWorkPreferenceController(context, lifecycle));
controllers.add(new RecentLocationRequestPreferenceController(context, fragment, lifecycle));
controllers.add(new LocationScanningPreferenceController(context));
controllers.add(new LocationServicePreferenceController(context, fragment, lifecycle));
controllers.add(new LocationFooterPreferenceController(context));
return controllers;
}
/** /**
* For Search. * For Search.
*/ */
public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER = public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
new BaseSearchIndexProvider(R.xml.location_settings) { new BaseSearchIndexProvider(R.xml.location_settings);
@Override
public List<AbstractPreferenceController> createPreferenceControllers(Context
context) {
return buildPreferenceControllers(context, null /* fragment */,
null /* lifecycle */);
}
};
} }

View File

@@ -26,18 +26,13 @@ import com.android.settings.R;
import com.android.settings.applications.appinfo.AppInfoDashboardFragment; import com.android.settings.applications.appinfo.AppInfoDashboardFragment;
import com.android.settings.core.SubSettingLauncher; import com.android.settings.core.SubSettingLauncher;
import com.android.settings.dashboard.DashboardFragment; import com.android.settings.dashboard.DashboardFragment;
import com.android.settingslib.core.lifecycle.Lifecycle;
import com.android.settingslib.location.RecentLocationApps; import com.android.settingslib.location.RecentLocationApps;
import com.android.settingslib.widget.apppreference.AppPreference; import com.android.settingslib.widget.apppreference.AppPreference;
import java.util.List; import java.util.List;
public class RecentLocationRequestPreferenceController extends LocationBasePreferenceController { public class RecentLocationRequestPreferenceController extends LocationBasePreferenceController {
/** Key for preference category "Recent location requests" */
private static final String KEY_RECENT_LOCATION_REQUESTS = "recent_location_requests";
@VisibleForTesting
static final String KEY_SEE_ALL_BUTTON = "recent_location_requests_see_all_button";
private final LocationSettings mFragment;
private final RecentLocationApps mRecentLocationApps; private final RecentLocationApps mRecentLocationApps;
private PreferenceCategory mCategoryRecentLocationRequests; private PreferenceCategory mCategoryRecentLocationRequests;
@@ -70,29 +65,15 @@ public class RecentLocationRequestPreferenceController extends LocationBasePrefe
} }
} }
public RecentLocationRequestPreferenceController(Context context, LocationSettings fragment, public RecentLocationRequestPreferenceController(Context context, String key) {
Lifecycle lifecycle) { super(context, key);
this(context, fragment, lifecycle, new RecentLocationApps(context)); mRecentLocationApps = new RecentLocationApps(context);
}
@VisibleForTesting
RecentLocationRequestPreferenceController(Context context, LocationSettings fragment,
Lifecycle lifecycle, RecentLocationApps recentApps) {
super(context, lifecycle);
mFragment = fragment;
mRecentLocationApps = recentApps;
}
@Override
public String getPreferenceKey() {
return KEY_RECENT_LOCATION_REQUESTS;
} }
@Override @Override
public void displayPreference(PreferenceScreen screen) { public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen); super.displayPreference(screen);
mCategoryRecentLocationRequests = mCategoryRecentLocationRequests = screen.findPreference(getPreferenceKey());
(PreferenceCategory) screen.findPreference(KEY_RECENT_LOCATION_REQUESTS);
} }
@Override @Override

View File

@@ -24,13 +24,8 @@ import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.dashboard.DashboardFragment; import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.search.BaseSearchIndexProvider; import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settingslib.core.AbstractPreferenceController;
import com.android.settingslib.core.lifecycle.Lifecycle;
import com.android.settingslib.search.SearchIndexable; import com.android.settingslib.search.SearchIndexable;
import java.util.ArrayList;
import java.util.List;
/** Dashboard Fragment to display all recent location requests, sorted by recency. */ /** Dashboard Fragment to display all recent location requests, sorted by recency. */
@SearchIndexable @SearchIndexable
public class RecentLocationRequestSeeAllFragment extends DashboardFragment { public class RecentLocationRequestSeeAllFragment extends DashboardFragment {
@@ -51,6 +46,14 @@ public class RecentLocationRequestSeeAllFragment extends DashboardFragment {
return MetricsEvent.RECENT_LOCATION_REQUESTS_ALL; return MetricsEvent.RECENT_LOCATION_REQUESTS_ALL;
} }
@Override
public void onAttach(Context context) {
super.onAttach(context);
mController = use(RecentLocationRequestSeeAllPreferenceController.class);
mController.init(this);
}
@Override @Override
protected int getPreferenceScreenResId() { protected int getPreferenceScreenResId() {
return R.xml.location_recent_requests_see_all; return R.xml.location_recent_requests_see_all;
@@ -61,11 +64,6 @@ public class RecentLocationRequestSeeAllFragment extends DashboardFragment {
return TAG; return TAG;
} }
@Override
protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
return buildPreferenceControllers(context, getSettingsLifecycle(), this);
}
@Override @Override
public boolean onOptionsItemSelected(MenuItem menuItem) { public boolean onOptionsItemSelected(MenuItem menuItem) {
switch (menuItem.getItemId()) { switch (menuItem.getItemId()) {
@@ -87,32 +85,6 @@ public class RecentLocationRequestSeeAllFragment extends DashboardFragment {
mHideSystemMenu.setVisible(mShowSystem); mHideSystemMenu.setVisible(mShowSystem);
} }
private static List<AbstractPreferenceController> buildPreferenceControllers(
Context context, Lifecycle lifecycle, RecentLocationRequestSeeAllFragment fragment) {
final List<AbstractPreferenceController> controllers = new ArrayList<>();
final RecentLocationRequestSeeAllPreferenceController controller =
new RecentLocationRequestSeeAllPreferenceController(context, lifecycle, fragment);
controllers.add(controller);
if (fragment != null) {
fragment.mController = controller;
}
return controllers;
}
/**
* For Search.
*/
public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
new BaseSearchIndexProvider(R.xml.location_recent_requests_see_all) {
@Override
public List<AbstractPreferenceController> getPreferenceControllers(Context
context) {
return buildPreferenceControllers(
context, /* lifecycle = */ null, /* fragment = */ null);
}
};
@Override @Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater); super.onCreateOptionsMenu(menu, inflater);
@@ -122,4 +94,10 @@ public class RecentLocationRequestSeeAllFragment extends DashboardFragment {
R.string.menu_hide_system); R.string.menu_hide_system);
updateMenu(); updateMenu();
} }
/**
* For Search.
*/
public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
new BaseSearchIndexProvider(R.xml.location_recent_requests_see_all);
} }

View File

@@ -22,44 +22,24 @@ import androidx.preference.Preference;
import androidx.preference.PreferenceCategory; import androidx.preference.PreferenceCategory;
import androidx.preference.PreferenceScreen; import androidx.preference.PreferenceScreen;
import com.android.settingslib.core.lifecycle.Lifecycle; import com.android.settings.R;
import com.android.settingslib.location.RecentLocationApps; import com.android.settingslib.location.RecentLocationApps;
import com.android.settingslib.widget.apppreference.AppPreference; import com.android.settingslib.widget.apppreference.AppPreference;
import java.util.List; import java.util.List;
import com.android.settings.R;
/** Preference controller for preference category displaying all recent location requests. */ /** Preference controller for preference category displaying all recent location requests. */
public class RecentLocationRequestSeeAllPreferenceController public class RecentLocationRequestSeeAllPreferenceController
extends LocationBasePreferenceController { extends LocationBasePreferenceController {
/** Key for preference category "All recent location requests" */
private static final String KEY_ALL_RECENT_LOCATION_REQUESTS = "all_recent_location_requests";
private final RecentLocationRequestSeeAllFragment mFragment;
private PreferenceCategory mCategoryAllRecentLocationRequests; private PreferenceCategory mCategoryAllRecentLocationRequests;
private RecentLocationApps mRecentLocationApps; private RecentLocationApps mRecentLocationApps;
private boolean mShowSystem = false; private boolean mShowSystem = false;
private Preference mPreference; private Preference mPreference;
public RecentLocationRequestSeeAllPreferenceController( public RecentLocationRequestSeeAllPreferenceController(Context context, String key) {
Context context, Lifecycle lifecycle, RecentLocationRequestSeeAllFragment fragment) { super(context, key);
this(context, lifecycle, fragment, new RecentLocationApps(context)); mRecentLocationApps = new RecentLocationApps(context);
}
@VisibleForTesting
RecentLocationRequestSeeAllPreferenceController(
Context context,
Lifecycle lifecycle,
RecentLocationRequestSeeAllFragment fragment,
RecentLocationApps recentLocationApps) {
super(context, lifecycle);
mFragment = fragment;
mRecentLocationApps = recentLocationApps;
}
@Override
public String getPreferenceKey() {
return KEY_ALL_RECENT_LOCATION_REQUESTS;
} }
@Override @Override
@@ -70,8 +50,7 @@ public class RecentLocationRequestSeeAllPreferenceController
@Override @Override
public void displayPreference(PreferenceScreen screen) { public void displayPreference(PreferenceScreen screen) {
super.displayPreference(screen); super.displayPreference(screen);
mCategoryAllRecentLocationRequests = mCategoryAllRecentLocationRequests = screen.findPreference(getPreferenceKey());
(PreferenceCategory) screen.findPreference(KEY_ALL_RECENT_LOCATION_REQUESTS);
} }
@Override @Override

View File

@@ -2,6 +2,9 @@ package com.android.settings.location;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.when;
import android.content.Context; import android.content.Context;
import android.location.LocationManager; import android.location.LocationManager;
import android.provider.Settings; import android.provider.Settings;
@@ -30,6 +33,7 @@ public class AppLocationPermissionPreferenceControllerTest {
private LifecycleOwner mLifecycleOwner; private LifecycleOwner mLifecycleOwner;
private Lifecycle mLifecycle; private Lifecycle mLifecycle;
private LocationManager mLocationManager; private LocationManager mLocationManager;
private LocationSettings mLocationSettings;
@Before @Before
public void setUp() { public void setUp() {
@@ -37,7 +41,10 @@ public class AppLocationPermissionPreferenceControllerTest {
mContext = RuntimeEnvironment.application; mContext = RuntimeEnvironment.application;
mLifecycleOwner = () -> mLifecycle; mLifecycleOwner = () -> mLifecycle;
mLifecycle = new Lifecycle(mLifecycleOwner); mLifecycle = new Lifecycle(mLifecycleOwner);
mController = new AppLocationPermissionPreferenceController(mContext, mLifecycle); mLocationSettings = spy(new LocationSettings());
when(mLocationSettings.getSettingsLifecycle()).thenReturn(mLifecycle);
mController = new AppLocationPermissionPreferenceController(mContext, "key");
mController.init(mLocationSettings);
mLocationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE); mLocationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
} }

View File

@@ -70,7 +70,7 @@ public class LocationFooterPreferenceControllerTest {
Context context = spy(RuntimeEnvironment.application); Context context = spy(RuntimeEnvironment.application);
when(context.getPackageManager()).thenReturn(mPackageManager); when(context.getPackageManager()).thenReturn(mPackageManager);
when(mPreferenceCategory.getContext()).thenReturn(context); when(mPreferenceCategory.getContext()).thenReturn(context);
mController = spy(new LocationFooterPreferenceController(context)); mController = spy(new LocationFooterPreferenceController(context, "key"));
when(mPackageManager.getResourcesForApplication(any(ApplicationInfo.class))) when(mPackageManager.getResourcesForApplication(any(ApplicationInfo.class)))
.thenReturn(mResources); .thenReturn(mResources);
when(mResources.getString(TEST_RES_ID)).thenReturn(TEST_TEXT); when(mResources.getString(TEST_RES_ID)).thenReturn(TEST_TEXT);

View File

@@ -69,6 +69,7 @@ public class LocationForWorkPreferenceControllerTest {
private LocationForWorkPreferenceController mController; private LocationForWorkPreferenceController mController;
private LifecycleOwner mLifecycleOwner; private LifecycleOwner mLifecycleOwner;
private Lifecycle mLifecycle; private Lifecycle mLifecycle;
private LocationSettings mLocationSettings;
@Before @Before
public void setUp() { public void setUp() {
@@ -77,10 +78,13 @@ public class LocationForWorkPreferenceControllerTest {
when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager); when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
mLifecycleOwner = () -> mLifecycle; mLifecycleOwner = () -> mLifecycle;
mLifecycle = new Lifecycle(mLifecycleOwner); mLifecycle = new Lifecycle(mLifecycleOwner);
mController = spy(new LocationForWorkPreferenceController(mContext, mLifecycle)); mLocationSettings = spy(new LocationSettings());
when(mLocationSettings.getSettingsLifecycle()).thenReturn(mLifecycle);
mController = spy(new LocationForWorkPreferenceController(mContext, "key"));
mController.init(mLocationSettings);
mockManagedProfile(); mockManagedProfile();
ReflectionHelpers.setField(mController, "mLocationEnabler", mEnabler); ReflectionHelpers.setField(mController, "mLocationEnabler", mEnabler);
when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference); when(mScreen.findPreference(any())).thenReturn(mPreference);
final String key = mController.getPreferenceKey(); final String key = mController.getPreferenceKey();
when(mPreference.getKey()).thenReturn(key); when(mPreference.getKey()).thenReturn(key);
when(mPreference.isVisible()).thenReturn(true); when(mPreference.isVisible()).thenReturn(true);

View File

@@ -38,7 +38,7 @@ public class LocationScanningPreferenceControllerTest {
@Before @Before
public void setUp() { public void setUp() {
mContext = RuntimeEnvironment.application; mContext = RuntimeEnvironment.application;
mController = new LocationScanningPreferenceController(mContext); mController = new LocationScanningPreferenceController(mContext, "key");
} }
@Test @Test

View File

@@ -15,6 +15,9 @@
*/ */
package com.android.settings.location; package com.android.settings.location;
import static com.android.settings.location.LocationServicePreferenceController.KEY_LOCATION_SERVICES;
import static com.android.settings.location.LocationServicePreferenceController.KEY_LOCATION_SERVICES_MANAGED;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
@@ -59,8 +62,6 @@ import java.util.Map;
@RunWith(RobolectricTestRunner.class) @RunWith(RobolectricTestRunner.class)
@Config(shadows = ShadowUserManager.class) @Config(shadows = ShadowUserManager.class)
public class LocationServicePreferenceControllerTest { public class LocationServicePreferenceControllerTest {
private static final String LOCATION_SERVICES_MANAGED_PROFILE_KEY =
"location_services_managed_profile";
@Mock(answer = Answers.RETURNS_DEEP_STUBS) @Mock(answer = Answers.RETURNS_DEEP_STUBS)
private LocationSettings mFragment; private LocationSettings mFragment;
@@ -86,14 +87,16 @@ public class LocationServicePreferenceControllerTest {
mContext = spy(RuntimeEnvironment.application); mContext = spy(RuntimeEnvironment.application);
mLifecycleOwner = () -> mLifecycle; mLifecycleOwner = () -> mLifecycle;
mLifecycle = new Lifecycle(mLifecycleOwner); mLifecycle = new Lifecycle(mLifecycleOwner);
mController = spy(new LocationServicePreferenceController( mController = spy(new LocationServicePreferenceController(mContext, KEY_LOCATION_SERVICES));
mContext, mFragment, mLifecycle, mSettingsInjector)); when(mFragment.getSettingsLifecycle()).thenReturn(mLifecycle);
mController.init(mFragment);
mController.mInjector = mSettingsInjector;
final String key = mController.getPreferenceKey(); final String key = mController.getPreferenceKey();
when(mScreen.findPreference(key)).thenReturn(mCategoryPrimary); when(mScreen.findPreference(key)).thenReturn(mCategoryPrimary);
when(mScreen.findPreference(LOCATION_SERVICES_MANAGED_PROFILE_KEY)).thenReturn( when(mScreen.findPreference(KEY_LOCATION_SERVICES_MANAGED)).thenReturn(
mCategoryManaged); mCategoryManaged);
when(mCategoryPrimary.getKey()).thenReturn(key); when(mCategoryPrimary.getKey()).thenReturn(key);
when(mCategoryManaged.getKey()).thenReturn(LOCATION_SERVICES_MANAGED_PROFILE_KEY); when(mCategoryManaged.getKey()).thenReturn(KEY_LOCATION_SERVICES_MANAGED);
when(mContext.getSystemService(Context.DEVICE_POLICY_SERVICE)) when(mContext.getSystemService(Context.DEVICE_POLICY_SERVICE))
.thenReturn(mDevicePolicyManager); .thenReturn(mDevicePolicyManager);
} }