Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 2229585e authored by Raff Tsai's avatar Raff Tsai
Browse files

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
parent a6198c25
Loading
Loading
Loading
Loading
+9 −5
Original line number Diff line number Diff line
@@ -10,9 +10,13 @@
     See the License for the specific language governing permissions and
     limitations under the License.
-->
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:settings="http://schemas.android.com/apk/res-auto"
    android:title="@string/location_category_recent_location_requests"
    android:key="recent_location_requests_see_all">
    <PreferenceCategory
            android:key="all_recent_location_requests"/>
        android:key="all_recent_location_requests"
        settings:controller="com.android.settings.location.RecentLocationRequestSeeAllPreferenceController"/>

</PreferenceScreen>
+57 −50
Original line number Diff line number Diff line
@@ -14,7 +14,8 @@
     limitations under the License.
-->

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:settings="http://schemas.android.com/apk/res-auto"
    android:key="location_settings"
    android:title="@string/location_settings_title"
@@ -22,7 +23,8 @@

    <PreferenceCategory
        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"
@@ -39,7 +41,8 @@
        <!-- 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">
            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"/>
@@ -47,31 +50,35 @@
        </Preference>

        <Preference
            android:fragment="com.android.settings.location.ScanningSettings"
            android:key="location_scanning"
            android:title="@string/location_scanning_screen_title"
                android:fragment="com.android.settings.location.ScanningSettings"/>
            settings:controller="com.android.settings.location.LocationScanningPreferenceController"/>

        <!-- 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:useAdminDisabledSummary="true"
                android:enabled="false"
                android:selectable="true" />
            settings:controller="com.android.settings.location.LocationForWorkPreferenceController"
            settings:useAdminDisabledSummary="true"/>

        <PreferenceCategory
            android:key="location_services"
                android:layout="@layout/preference_category_no_label"/>
            android:layout="@layout/preference_category_no_label"
            settings:controller="com.android.settings.location.LocationServicePreferenceController"/>

        <!-- This preference gets removed if there is no managed profile -->
        <PreferenceCategory
                android:title="@string/managed_profile_location_services"
                android:key="location_services_managed_profile" />
            android:key="location_services_managed_profile"
            android:title="@string/managed_profile_location_services"/>

    </PreferenceCategory>

    <PreferenceCategory
        android:key="location_footer"
        android:layout="@layout/preference_category_no_label"
            settings:allowDividerAbove="false"/>
        settings:allowDividerAbove="false"
        settings:controller="com.android.settings.location.LocationFooterPreferenceController"/>
</PreferenceScreen>
+5 −11
Original line number Diff line number Diff line
@@ -16,7 +16,6 @@ import androidx.preference.Preference;
import com.android.settings.R;
import com.android.settings.Utils;
import com.android.settings.core.PreferenceControllerMixin;
import com.android.settingslib.core.lifecycle.Lifecycle;

import java.util.Arrays;
import java.util.List;
@@ -25,7 +24,6 @@ import java.util.concurrent.atomic.AtomicInteger;
public class AppLocationPermissionPreferenceController extends
        LocationBasePreferenceController implements PreferenceControllerMixin {

    private static final String KEY_APP_LEVEL_PERMISSIONS = "app_level_permissions";
    /** Total number of apps that has location permission. */
    @VisibleForTesting
    int mNumTotal = -1;
@@ -40,20 +38,16 @@ public class AppLocationPermissionPreferenceController extends
    private final LocationManager mLocationManager;
    private Preference mPreference;

    public AppLocationPermissionPreferenceController(Context context, Lifecycle lifecycle) {
        super(context, lifecycle);
    public AppLocationPermissionPreferenceController(Context context, String key) {
        super(context, key);
        mLocationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    }

    @Override
    public String getPreferenceKey() {
        return KEY_APP_LEVEL_PERMISSIONS;
    }

    @Override
    public boolean isAvailable() {
    public int getAvailabilityStatus() {
        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
+29 −12
Original line number Diff line number Diff line
@@ -16,29 +16,46 @@ package com.android.settings.location;
import android.content.Context;
import android.os.UserManager;

import com.android.settings.core.PreferenceControllerMixin;
import com.android.settingslib.core.AbstractPreferenceController;
import com.android.settings.core.BasePreferenceController;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settingslib.core.lifecycle.Lifecycle;

/**
 * A base controller for preferences that listens to location settings change and modifies location
 * settings.
 */
public abstract class LocationBasePreferenceController extends AbstractPreferenceController
        implements PreferenceControllerMixin, LocationEnabler.LocationModeChangeListener {
public abstract class LocationBasePreferenceController extends BasePreferenceController
        implements LocationEnabler.LocationModeChangeListener {

    protected final UserManager mUserManager;
    protected final LocationEnabler mLocationEnabler;
    protected UserManager mUserManager;
    protected LocationEnabler mLocationEnabler;
    protected DashboardFragment mFragment;
    protected Lifecycle mLifecycle;

    public LocationBasePreferenceController(Context context, Lifecycle lifecycle) {
        super(context);
        mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
        mLocationEnabler = new LocationEnabler(context, this /* listener */, lifecycle);
    /**
     * Constructor of LocationBasePreferenceController. {@link BasePreferenceController} uses
     * reflection to create controller, all controllers extends {@link BasePreferenceController}
     * 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
    public boolean isAvailable() {
        return true;
    public int getAvailabilityStatus() {
        return AVAILABLE;
    }

}
+4 −13
Original line number Diff line number Diff line
@@ -13,7 +13,6 @@
 */
package com.android.settings.location;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
@@ -24,7 +23,6 @@ import android.content.pm.ResolveInfo;
import android.location.LocationManager;
import android.util.Log;

import androidx.annotation.VisibleForTesting;
import androidx.preference.Preference;
import androidx.preference.PreferenceCategory;

@@ -41,23 +39,16 @@ import java.util.List;
public class LocationFooterPreferenceController extends LocationBasePreferenceController {

    private static final String TAG = "LocationFooter";
    private static final String KEY_LOCATION_FOOTER = "location_footer";
    private static final Intent INJECT_INTENT =
            new Intent(LocationManager.SETTINGS_FOOTER_DISPLAYED_ACTION);

    private final PackageManager mPackageManager;

    public LocationFooterPreferenceController(Context context) {
        // we don't care location mode changes, so pass in a null lifecycle to disable listening
        super(context, null);
    public LocationFooterPreferenceController(Context context, String key) {
        super(context, key);
        mPackageManager = context.getPackageManager();
    }

    @Override
    public String getPreferenceKey() {
        return KEY_LOCATION_FOOTER;
    }

    /**
     * Insert footer preferences.
     */
@@ -97,8 +88,8 @@ public class LocationFooterPreferenceController extends LocationBasePreferenceCo
     * inject.
     */
    @Override
    public boolean isAvailable() {
        return !getFooterData().isEmpty();
    public int getAvailabilityStatus() {
        return !getFooterData().isEmpty() ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
    }

    /**
Loading