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

Commit 2b1a88da authored by Matthew Fritze's avatar Matthew Fritze
Browse files

Simplify InlineSwitchPayloads and generalize get/set method

InlineSwitchPayload now assumes that all switches will be
stored as 1 or 0, which simplifies the code.

Moves Availability into ResultPayload, so that custom
payloads can be created to set/get values which are more
complicated than stotring ints (like bluetooth or DnD),
and give more expressive reasons when unavailable.

Bug: 62022517
Test: make RunSettingsRoboTests
Change-Id: I87e6fc041bfd398e7daf6e6e479d502930d36f0f
parent 63b013ea
Loading
Loading
Loading
Loading
+2 −8
Original line number Diff line number Diff line
@@ -19,15 +19,12 @@ import android.provider.Settings;
import android.support.v14.preference.SwitchPreference;
import android.support.v7.preference.Preference;

import android.util.ArrayMap;
import com.android.settings.core.PreferenceController;
import com.android.settings.search.DatabaseIndexingUtils;
import com.android.settings.search.InlineSwitchPayload;
import com.android.settings.search.ResultPayload;
import com.android.settings.R;

import java.util.Map;

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;
@@ -71,15 +68,12 @@ public class AutoBrightnessPreferenceController extends PreferenceController imp

    @Override
    public ResultPayload getResultPayload() {
        final Map<Integer, Boolean> valueMap = new ArrayMap<>();
        valueMap.put(SCREEN_BRIGHTNESS_MODE_AUTOMATIC, true);
        valueMap.put(SCREEN_BRIGHTNESS_MODE_MANUAL, false);

        final Intent intent = DatabaseIndexingUtils.buildSubsettingIntent(mContext,
                getClass().getName(), mAutoBrightnessKey,
                mContext.getString(R.string.display_settings));

        return new InlineSwitchPayload(SCREEN_BRIGHTNESS_MODE,
                ResultPayload.SettingsSource.SYSTEM, valueMap, intent);
                ResultPayload.SettingsSource.SYSTEM, SCREEN_BRIGHTNESS_MODE_AUTOMATIC, intent,
                isAvailable());
    }
}
+20 −19
Original line number Diff line number Diff line
@@ -58,9 +58,9 @@ import static com.android.settings.search.SearchResult.TOP_RANK;
 * - {@link Drawable} icon
 * - {@link ResultPayload} payload
 */
class CursorToSearchResultConverter {
public class CursorToSearchResultConverter {

    private final String TAG = "CursorConverter";
    private static final String TAG = "CursorConverter";

    private final Context mContext;

@@ -103,6 +103,23 @@ class CursorToSearchResultConverter {
        return results;
    }

    public static ResultPayload getUnmarshalledPayload(byte[] marshalledPayload,
            int payloadType) {
        try {
            switch (payloadType) {
                case ResultPayload.PayloadType.INTENT:
                    return ResultPayloadUtils.unmarshall(marshalledPayload,
                            ResultPayload.CREATOR);
                case ResultPayload.PayloadType.INLINE_SWITCH:
                    return ResultPayloadUtils.unmarshall(marshalledPayload,
                            InlineSwitchPayload.CREATOR);
            }
        } catch (BadParcelableException e) {
            Log.w(TAG, "Error creating parcelable: " + e);
        }
        return null;
    }

    private SearchResult buildSingleSearchResultFromCursor(SiteMapManager sitemapManager,
            Map<String, Context> contextMap, Cursor cursor, int baseRank) {
        final int docId = cursor.getInt(COLUMN_INDEX_ID);
@@ -162,22 +179,6 @@ class CursorToSearchResultConverter {
        return icon;
    }

    private ResultPayload getUnmarshalledPayload(byte[] unmarshalledPayload, int payloadType) {
        try {
            switch (payloadType) {
                case ResultPayload.PayloadType.INTENT:
                    return ResultPayloadUtils.unmarshall(unmarshalledPayload,
                            ResultPayload.CREATOR);
                case ResultPayload.PayloadType.INLINE_SWITCH:
                    return ResultPayloadUtils.unmarshall(unmarshalledPayload,
                            InlineSwitchPayload.CREATOR);
            }
        } catch (BadParcelableException e) {
            Log.w(TAG, "Error creating parcelable: " + e);
        }
        return null;
    }

    private List<String> getBreadcrumbs(SiteMapManager siteMapManager, Cursor cursor) {
        final String screenTitle = cursor.getString(COLUMN_INDEX_SCREEN_TITLE);
        final String screenClass = cursor.getString(COLUMN_INDEX_CLASS_NAME);
+1 −0
Original line number Diff line number Diff line
@@ -803,6 +803,7 @@ public class DatabaseIndexingManager {
                        entries = XmlParserUtils.getDataEntries(context, attrs);
                    }

                    // TODO (b/62254931) index primitives instead of payload
                    payload = DatabaseIndexingUtils.getPayloadFromUriMap(controllerUriMap, key);
                    childFragment = XmlParserUtils.getDataChildFragment(context, attrs);

+73 −10
Original line number Diff line number Diff line
@@ -19,30 +19,93 @@ package com.android.settings.search;

import android.content.Intent;

import android.content.Context;
import android.os.Parcel;
import com.android.internal.annotations.VisibleForTesting;

/**
 * Abstract Payload for inline settings results.
 */
public abstract class InlinePayload extends ResultPayload {

    public static final int FALSE = 0;
    public static final int TRUE = 1;

    /**
     * Defines the URI to access and store the Setting the inline result represents
     * Defines the key to access and store the Setting the inline result represents.
     */
    public String settingsUri;
    @VisibleForTesting
    final String mSettingKey;

    /**
     * The UI type for the inline result.
     */
    @PayloadType public int inlineType;
    @PayloadType final int mInlineType;

    /**
     * Defines where the Setting is stored.
     */
    @SettingsSource public int settingSource;
    @SettingsSource final int mSettingSource;

    /**
     * True when the setting is available for the device.
     */
    final boolean mIsDeviceSupported;

    public InlinePayload(String uri, @PayloadType int type, @SettingsSource int source,
            Intent intent) {
    /**
     * @param key uniquely identifies the stored setting.
     * @param payloadType of the setting being stored.
     * @param source of the setting. Used to determine where to get and set the setting.
     * @param intent to the setting page.
     * @param isDeviceSupported is true when the setting is valid for the given device.
     */
    public InlinePayload(String key, @PayloadType int payloadType, @SettingsSource int source,
            Intent intent, boolean isDeviceSupported) {
        super(intent);
        settingsUri = uri;
        inlineType = type;
        settingSource = source;
        mSettingKey = key;
        mInlineType = payloadType;
        mSettingSource = source;
        mIsDeviceSupported = isDeviceSupported;
    }

    InlinePayload(Parcel parcel) {
        super((Intent) parcel.readParcelable(Intent.class.getClassLoader()));
        mSettingKey = parcel.readString();
        mInlineType = parcel.readInt();
        mSettingSource = parcel.readInt();
        mIsDeviceSupported = parcel.readInt() == TRUE;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        super.writeToParcel(dest, flags);
        dest.writeString(mSettingKey);
        dest.writeInt(mInlineType);
        dest.writeInt(mSettingSource);
        dest.writeInt(mIsDeviceSupported ? TRUE : FALSE);
    }

    /**
     * @returns the status of the underlying setting. See {@link ResultPayload.Availability} for
     * possible values.
     */
    @Availability public int getAvailability() {
        if (mIsDeviceSupported) {
            return Availability.AVAILABLE;
        }
        return Availability.DISABLED_UNSUPPORTED;
    }

    /**
     * @returns the current value of the setting.
     */
    public abstract int getValue(Context context);

    /**
     * Attempts to set the setting value.
     *
     * @param newValue is the requested new value for the setting.
     * @returns true when the setting was changed, and false otherwise.
     */
    public abstract boolean setValue(Context context, int newValue);
}
 No newline at end of file
+55 −60
Original line number Diff line number Diff line
@@ -23,37 +23,41 @@ import android.os.Parcel;
import android.os.Parcelable;
import android.provider.Settings;

import java.util.Map;

/**
 * Payload for inline Switch results. Mappings from integer to boolean.
 */
public class InlineSwitchPayload extends InlinePayload {

    /**
     * Maps Inline values to UI-consumable Values.
     * For example, if you have a switch preference whose values are stored as ints, the two valid
     * list of mappings would be:
     * < (0,True), (1, false) >
     * < (1,True), (0, false) >
     * Provides a mapping for how switches are stored.
     * If mIsStandard is true, then (0 == false) and (1 == true)
     * If mIsStandard is false, then (1 == false) and (0 == true)
     */
    public final Map<Integer, Boolean> valueMap;
    private boolean mIsStandard;

    public InlineSwitchPayload(String newUri, @SettingsSource int settingsSource,
            Map<Integer, Boolean> map, Intent intent) {
        super(newUri, PayloadType.INLINE_SWITCH, settingsSource, intent);
        valueMap = map;
    /**
     *
     * @param key uniquely identifies the stored setting.
     * @param source of the setting. Used to determine where to get and set the setting.
     * @param onValue is the value stored as on for the switch. Should be 0 or 1.
     * @param intent to the setting page.
     * @param isDeviceSupported is true when the setting is valid for the given device.
     */
    public InlineSwitchPayload(String key, @SettingsSource int source,
            int onValue, Intent intent, boolean isDeviceSupported) {
        super(key, PayloadType.INLINE_SWITCH, source, intent, isDeviceSupported);
        // If on is stored as TRUE then the switch is standard.
        mIsStandard = onValue == TRUE;
    }

    private InlineSwitchPayload(Parcel in) {
        super(in.readString() /* Uri */ , in.readInt() /* Payload Type */,
                in.readInt() /* Settings Source */,
                (Intent) in.readParcelable(Intent.class.getClassLoader()) /* Intent */);
        valueMap = in.readHashMap(Integer.class.getClassLoader());
        super(in);
        mIsStandard = in.readInt() == TRUE;
    }

    @Override
    public int getType() {
        return inlineType;
        return mInlineType;
    }

    @Override
@@ -63,11 +67,8 @@ public class InlineSwitchPayload extends InlinePayload {

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(settingsUri);
        dest.writeInt(inlineType);
        dest.writeInt(settingSource);
        dest.writeParcelable(mIntent, flags);
        dest.writeMap(valueMap);
        super.writeToParcel(dest, flags);
        dest.writeInt(mIsStandard ? TRUE : FALSE);
    }

    public static final Parcelable.Creator<InlineSwitchPayload> CREATOR =
@@ -83,71 +84,65 @@ public class InlineSwitchPayload extends InlinePayload {
        }
    };

    public boolean getSwitchValue(Context context) {
        if (valueMap == null) {
            throw new IllegalStateException("Value map is null");
        }

    @Override
    public int getValue(Context context) {
        int settingsValue = -1;
        switch(settingSource) {
        switch(mSettingSource) {
            case SettingsSource.SECURE:
                settingsValue = Settings.Secure.getInt(context.getContentResolver(),
                        settingsUri, 0);
                        mSettingKey, -1);
                break;
            case SettingsSource.SYSTEM:
                settingsValue = Settings.System.getInt(context.getContentResolver(),
                        settingsUri, 0);
                        mSettingKey, -1);
                break;

            case SettingsSource.GLOBAL:
                settingsValue = Settings.Global.getInt(context.getContentResolver(),
                        settingsUri, 0);
                        mSettingKey, -1);
                break;
        }

        if (settingsValue == -1) {
            throw new IllegalStateException("Unable to find setting from uri: "
                    + settingsUri.toString());
                    + mSettingKey.toString());
        }

        for (Integer key : valueMap.keySet()) {
            if ((key == settingsValue)) {
                return valueMap.get(key);
            }
        }
        settingsValue = standardizeInput(settingsValue);

        throw new IllegalStateException("No results matched the key: " + settingsValue);
        return settingsValue;
    }

    public void setSwitchValue(Context context, boolean isChecked) {
        if (valueMap == null) {
            throw new IllegalStateException("Value map is null");
        }
        int switchValue = -1;

        for (Map.Entry<Integer, Boolean> pair : valueMap.entrySet()) {
            if (pair.getValue() == isChecked) {
                switchValue = pair.getKey();
                break;
            }
    @Override
    public boolean setValue(Context context, int newValue) {
        if (newValue != 0 && newValue != 1) {
            throw new IllegalArgumentException("newValue should be 0 for off and 1 for on."
                    + "The passed value was: " + newValue);
        }

        if (switchValue == -1) {
            throw new IllegalStateException("Switch value is not set");
        }
        newValue = standardizeInput(newValue);

        switch(settingSource) {
        switch(mSettingSource) {
            case SettingsSource.GLOBAL:
                Settings.Global.putInt(context.getContentResolver(), settingsUri, switchValue);
                return;
                return Settings.Global.putInt(context.getContentResolver(), mSettingKey, newValue);
            case SettingsSource.SECURE:
                Settings.Secure.putInt(context.getContentResolver(), settingsUri, switchValue);
                return;
                return Settings.Secure.putInt(context.getContentResolver(), mSettingKey, newValue);
            case SettingsSource.SYSTEM:
                Settings.System.putInt(context.getContentResolver(), settingsUri, switchValue);
                return;
                return Settings.System.putInt(context.getContentResolver(), mSettingKey, newValue);
            case SettingsSource.UNKNOWN:
                return;
                return false;
        }

        return false;
    }

    public boolean isStandard() {
        return mIsStandard;
    }

    private int standardizeInput(int value) {
        return mIsStandard
                ? value
                : 1 - value;
    }
}
Loading