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

Commit be3b9ffe authored by Neil Fuller's avatar Neil Fuller Committed by Android (Google) Code Review
Browse files

Merge "Fix review comments location_time_zone_detection"

parents a14d7504 1affbfff
Loading
Loading
Loading
Loading
+4 −1
Original line number Diff line number Diff line
@@ -14,12 +14,15 @@
     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_time_zone_detection_screen_title">

    <SwitchPreference
        android:title="@string/location_time_zone_detection_enabled_title"
        android:summary="@string/location_time_zone_detection_enabled_description"
        settings:controller="com.android.settings.location.TimeZoneDetectionTogglePreferenceController"
        android:key="location_time_zone_detection_enabled" />

</PreferenceScreen>
+0 −2
Original line number Diff line number Diff line
@@ -58,8 +58,6 @@ public class LocationPersonalSettings extends DashboardFragment {
                RecentLocationRequestPreferenceController.class);
        controller.init(this);
        controller.setProfileType(profileType);

        use(LocationTimeZoneDetectionPreferenceController.class);
    }

    @Override
+0 −1
Original line number Diff line number Diff line
@@ -88,7 +88,6 @@ public class LocationSettings extends DashboardFragment {
        use(LocationFooterPreferenceController.class).init(this);
        use(LocationForWorkPreferenceController.class).init(this);
        use(LocationServiceForWorkPreferenceController.class).init(this);
        use(LocationTimeZoneDetectionPreferenceController.class);
    }

    @Override
+1 −24
Original line number Diff line number Diff line
@@ -16,17 +16,12 @@
package com.android.settings.location;

import android.app.settings.SettingsEnums;
import android.content.Context;

import com.android.settings.R;
import com.android.settings.dashboard.DashboardFragment;
import com.android.settings.search.BaseSearchIndexProvider;
import com.android.settingslib.core.AbstractPreferenceController;
import com.android.settingslib.search.SearchIndexable;

import java.util.ArrayList;
import java.util.List;

/**
 * The controller for the "location time zone detection" screen.
 */
@@ -49,27 +44,9 @@ public class TimeZoneDetectionSettings extends DashboardFragment {
        return TAG;
    }

    @Override
    protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
        return buildPreferenceControllers(context);
    }

    private static List<AbstractPreferenceController> buildPreferenceControllers(Context context) {
        final List<AbstractPreferenceController> controllers = new ArrayList<>();
        controllers.add(new TimeZoneDetectionPreferenceController(context));
        return controllers;
    }

    /**
     * For Search.
     */
    public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
            new BaseSearchIndexProvider(R.xml.location_time_zone_detection) {

                @Override
                public List<AbstractPreferenceController> createPreferenceControllers(Context
                        context) {
                    return buildPreferenceControllers(context);
                }
            };
            new BaseSearchIndexProvider(R.xml.location_time_zone_detection);
}
+59 −0
Original line number Diff line number Diff line
@@ -20,78 +20,40 @@ import android.app.time.TimeManager;
import android.app.time.TimeZoneCapabilitiesAndConfig;
import android.app.time.TimeZoneConfiguration;
import android.content.Context;
import android.text.TextUtils;

import androidx.preference.Preference;
import androidx.preference.SwitchPreference;

import com.android.settingslib.core.AbstractPreferenceController;
import com.android.settings.core.TogglePreferenceController;

/**
 * The controller for the "location time zone detection" switch on the location time zone detection
 * screen.
 */
public class TimeZoneDetectionPreferenceController extends AbstractPreferenceController {

    private static final String KEY_LOCATION_TIME_ZONE_DETECTION_ENABLED =
            "location_time_zone_detection_enabled";
public class TimeZoneDetectionTogglePreferenceController extends TogglePreferenceController {

    private final TimeManager mTimeManager;

    public TimeZoneDetectionPreferenceController(Context context) {
        super(context);
    public TimeZoneDetectionTogglePreferenceController(Context context, String key) {
        super(context, key);
        mTimeManager = context.getSystemService(TimeManager.class);
    }

    @Override
    public boolean isAvailable() {
        return true;
    public int getAvailabilityStatus() {
        return AVAILABLE;
    }

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

    @Override
    public void updateState(Preference preference) {
    public boolean isChecked() {
        TimeZoneCapabilitiesAndConfig capabilitiesAndConfig =
                mTimeManager.getTimeZoneCapabilitiesAndConfig();
        setPreferenceUiState((SwitchPreference) preference, capabilitiesAndConfig);
        TimeZoneConfiguration configuration = capabilitiesAndConfig.getConfiguration();
        return configuration.isGeoDetectionEnabled();
    }

    @Override
    public boolean handlePreferenceTreeClick(Preference preference) {
        if (TextUtils.equals(preference.getKey(), KEY_LOCATION_TIME_ZONE_DETECTION_ENABLED)) {
            SwitchPreference switchPreference = (SwitchPreference) preference;
            final boolean switchState = switchPreference.isChecked();

            // Update the settings to match the UI.
    public boolean setChecked(boolean isChecked) {
        TimeZoneConfiguration configuration = new TimeZoneConfiguration.Builder()
                    .setGeoDetectionEnabled(switchState)
                .setGeoDetectionEnabled(isChecked)
                .build();

            // The return value is ignored, but the current state is read back below ensuring it
            // does not matter.
            mTimeManager.updateTimeZoneConfiguration(configuration);

            // Configure the UI preference state from the configuration. This means that even in the
            // unlikely event that the update failed, the UI should reflect current settings.
            setPreferenceUiState(switchPreference, mTimeManager.getTimeZoneCapabilitiesAndConfig());

            return true;
        }
        return false;
    }

    /**
     * Sets the switch's checked state from the supplied {@link TimeZoneCapabilitiesAndConfig}.
     */
    @android.annotation.UiThread
    private void setPreferenceUiState(SwitchPreference preference,
            TimeZoneCapabilitiesAndConfig timeZoneCapabilitiesAndConfig) {
        TimeZoneConfiguration configuration = timeZoneCapabilitiesAndConfig.getConfiguration();
        boolean checked = configuration.isGeoDetectionEnabled();
        preference.setChecked(checked);
        return mTimeManager.updateTimeZoneConfiguration(configuration);
    }
}
Loading