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

Commit 96c1ecd5 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Add SettingsPrefController for Slices"

parents b1edae2d 3a416836
Loading
Loading
Loading
Loading
+159 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the
 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */
package com.android.settings.core;

import android.annotation.IntDef;
import android.app.slice.Slice;
import android.content.Context;
import android.support.v7.preference.Preference;
import android.text.TextUtils;
import android.util.Log;


import com.android.settings.core.PreferenceControllerMixin;
import com.android.settings.search.ResultPayload;
import com.android.settings.search.SearchIndexableRaw;
import com.android.settingslib.core.AbstractPreferenceController;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.List;

/**
 * Abstract class to consolidate utility between preference controllers and act as an interface
 * for Slices. The abstract classes that inherit from this class will act as the direct interfaces
 * for each type when plugging into Slices.
 */
public abstract class BasePreferenceController extends AbstractPreferenceController {

    private static final String TAG = "SettingsPrefController";

    @Retention(RetentionPolicy.SOURCE)
    @IntDef({AVAILABLE, DISABLED_UNSUPPORTED, DISABLED_FOR_USER, DISABLED_DEPENDENT_SETTING,
            UNAVAILABLE_UNKNOWN})
    public @interface AvailabilityStatus {
    }

    /**
     * The setting is available.
     */
    public static final int AVAILABLE = 0;

    /**
     * The setting is not supported by the device.
     */
    public static final int DISABLED_UNSUPPORTED = 1;

    /**
     * The setting cannot be changed by the current user.
     */
    public static final int DISABLED_FOR_USER = 2;

    /**
     * The setting has a dependency in the Settings App which is currently blocking access.
     */
    public static final int DISABLED_DEPENDENT_SETTING = 3;

    /**
     * A catch-all case for internal errors and inexplicable unavailability.
     */
    public static final int UNAVAILABLE_UNKNOWN = 4;

    private final String mPreferenceKey;

    public BasePreferenceController(Context context, String preferenceKey) {
        super(context);
        mPreferenceKey = preferenceKey;
    }

    /**
     * @return {@AvailabilityStatus} for the Setting. This status is used to determine if the
     * Setting should be shown or disabled in Settings. Further, it can be used to produce
     * appropriate error / warning Slice in the case of unavailability.
     * </p>
     * The status is used for the convenience methods: {@link #isAvailable()},
     * {@link #isSupported()}
     */
    @AvailabilityStatus
    public abstract int getAvailabilityStatus();

    /**
     * @return A slice for the corresponding setting.
     */
    public abstract Slice getSettingSlice();

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

    @Override
    public final boolean isAvailable() {
        return getAvailabilityStatus() == AVAILABLE;
    }

    /**
     * @return {@code false} if the setting is not applicable to the device. This covers both
     * settings which were only introduced in future versions of android, or settings that have
     * hardware dependencies.
     * </p>
     * Note that a return value of {@code true} does not mean that the setting is available.
     */
    public final boolean isSupported() {
        return getAvailabilityStatus() != DISABLED_UNSUPPORTED;
    }

    /**
     * Updates non-indexable keys for search provider.
     *
     * Called by SearchIndexProvider#getNonIndexableKeys
     */
    public void updateNonIndexableKeys(List<String> keys) {
        if (this instanceof AbstractPreferenceController) {
            if (!isAvailable()) {
                final String key = getPreferenceKey();
                if (TextUtils.isEmpty(key)) {
                    Log.w(TAG,
                            "Skipping updateNonIndexableKeys due to empty key " + this.toString());
                    return;
                }
                keys.add(key);
            }
        }
    }

    /**
     * Updates raw data for search provider.
     *
     * Called by SearchIndexProvider#getRawDataToIndex
     */
    public void updateRawDataToIndex(List<SearchIndexableRaw> rawData) {
    }

    /**
     * @return the {@link ResultPayload} corresponding to the search result type for the preference.
     * TODO (b/69808376) Remove this method.
     * Do not extend this method. It will not launch with P.
     */
    @Deprecated
    public ResultPayload getResultPayload() {
        return null;
    }

    // TODO (b/69380366) Add Method to get preference UI

    // TODO (b/69380464) Add method to get intent

    // TODO (b/69380560) Add method to get broadcast intent
}
 No newline at end of file
+5 −0
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@ import java.util.List;

/**
 * A controller mixin that adds mobile settings specific functionality
 * TODO (b/69808530) Replace with BasePreferenceController.
 */
public interface PreferenceControllerMixin {

@@ -60,7 +61,11 @@ public interface PreferenceControllerMixin {

    /**
     * @return the {@link ResultPayload} corresponding to the search result type for the preference.
     *
     * Do not rely on this method for intent-based or inline results. It will be removed in the
     * unbundling effort.
     */
    @Deprecated
    default ResultPayload getResultPayload() {
        return null;
    }
+64 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the
 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */
package com.android.settings.core;

import android.app.slice.Slice;
import android.content.Context;
import android.support.v14.preference.SwitchPreference;
import android.support.v7.preference.Preference;

/**
 * Abstract class that consolidates logic for updating toggle controllers.
 * It automatically handles the getting and setting of the switch UI element.
 * Children of this class implement methods to get and set the underlying value of the setting.
 */
public abstract class TogglePreferenceController extends BasePreferenceController implements
        Preference.OnPreferenceChangeListener {

    private static final String TAG = "TogglePrefController";

    public TogglePreferenceController(Context context, String preferenceKey) {
        super(context, preferenceKey);
    }

    /**
     * @return {@code true} if the Setting is enabled.
     */
    public abstract boolean isChecked();

    /**
     * Set the Setting to {@param isChecked}
     *
     * @param isChecked Is {@true} when the setting should be enabled.
     */
    public abstract void setChecked(boolean isChecked);

    @Override
    public final void updateState(Preference preference) {
        ((SwitchPreference) preference).setChecked(isChecked());
    }

    @Override
    public final boolean onPreferenceChange(Preference preference, Object newValue) {
        boolean auto = (Boolean) newValue;
        setChecked(auto);
        return true;
    }

    @Override
    public Slice getSettingSlice() {
        // TODO
        return null;
    }
}
 No newline at end of file
+17 −28
Original line number Diff line number Diff line
@@ -16,65 +16,54 @@ package com.android.settings.display;
import android.content.Context;
import android.content.Intent;
import android.provider.Settings;
import android.support.v14.preference.SwitchPreference;
import android.support.v7.preference.Preference;

import com.android.settings.DisplaySettings;
import com.android.settings.core.PreferenceControllerMixin;
import com.android.settings.core.TogglePreferenceController;
import com.android.settings.search.DatabaseIndexingUtils;
import com.android.settings.search.InlineSwitchPayload;
import com.android.settings.search.ResultPayload;
import com.android.settings.R;
import com.android.settingslib.core.AbstractPreferenceController;

import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE;
import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL;


public class AutoBrightnessPreferenceController extends AbstractPreferenceController implements
        PreferenceControllerMixin, Preference.OnPreferenceChangeListener {

    private final String mAutoBrightnessKey;
public class AutoBrightnessPreferenceController extends TogglePreferenceController {

    private final String SYSTEM_KEY = SCREEN_BRIGHTNESS_MODE;
    private final int DEFAULT_VALUE = SCREEN_BRIGHTNESS_MODE_MANUAL;

    public AutoBrightnessPreferenceController(Context context, String key) {
        super(context);
        mAutoBrightnessKey = key;
    }

    @Override
    public boolean isAvailable() {
        return mContext.getResources().getBoolean(
                com.android.internal.R.bool.config_automatic_brightness_available);
        super(context, key);
    }

    @Override
    public String getPreferenceKey() {
        return mAutoBrightnessKey;
    public boolean isChecked() {
        return Settings.System.getInt(mContext.getContentResolver(),
                SYSTEM_KEY, DEFAULT_VALUE) != DEFAULT_VALUE;
    }

    @Override
    public void updateState(Preference preference) {
        int brightnessMode = Settings.System.getInt(mContext.getContentResolver(),
                SYSTEM_KEY, DEFAULT_VALUE);
        ((SwitchPreference) preference).setChecked(brightnessMode != DEFAULT_VALUE);
    public void setChecked(boolean isChecked) {
        Settings.System.putInt(mContext.getContentResolver(), SYSTEM_KEY,
                isChecked ? SCREEN_BRIGHTNESS_MODE_AUTOMATIC : DEFAULT_VALUE);
    }

    @Override
    public boolean onPreferenceChange(Preference preference, Object newValue) {
        boolean auto = (Boolean) newValue;
        Settings.System.putInt(mContext.getContentResolver(), SYSTEM_KEY,
                auto ? SCREEN_BRIGHTNESS_MODE_AUTOMATIC : DEFAULT_VALUE);
        return true;
    @AvailabilityStatus
    public int getAvailabilityStatus() {
        return mContext.getResources().getBoolean(
                com.android.internal.R.bool.config_automatic_brightness_available)
                ? AVAILABLE
                : DISABLED_UNSUPPORTED;
    }

    @Override
    public ResultPayload getResultPayload() {
        // TODO remove result payload
        final Intent intent = DatabaseIndexingUtils.buildSearchResultPageIntent(mContext,
                DisplaySettings.class.getName(), mAutoBrightnessKey,
                DisplaySettings.class.getName(), getPreferenceKey(),
                mContext.getString(R.string.display_settings));

        return new InlineSwitchPayload(SYSTEM_KEY,
+4 −0
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@ import android.util.AttributeSet;
import android.util.Log;
import android.util.Xml;

import com.android.settings.core.BasePreferenceController;
import com.android.settings.core.PreferenceControllerMixin;
import com.android.settingslib.core.AbstractPreferenceController;

@@ -71,6 +72,9 @@ public class BaseSearchIndexProvider implements Indexable.SearchIndexProvider {
                if (controller instanceof PreferenceControllerMixin) {
                    ((PreferenceControllerMixin) controller)
                            .updateNonIndexableKeys(nonIndexableKeys);
                } else if (controller instanceof BasePreferenceController) {
                    ((BasePreferenceController) controller).updateNonIndexableKeys(
                            nonIndexableKeys);
                } else {
                    throw new IllegalStateException(controller.getClass().getName()
                            + " must implement " + PreferenceControllerMixin.class.getName());
Loading