Merge "Simplify InlineSwitchPayloads and generalize get/set method"
This commit is contained in:
committed by
Android (Google) Code Review
commit
3c292444b0
@@ -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());
|
||||
}
|
||||
}
|
||||
|
@@ -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);
|
||||
@@ -209,4 +210,4 @@ class CursorToSearchResultConverter {
|
||||
}
|
||||
return baseRank;
|
||||
}
|
||||
}
|
||||
}
|
@@ -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);
|
||||
|
||||
|
@@ -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;
|
||||
|
||||
public InlinePayload(String uri, @PayloadType int type, @SettingsSource int source,
|
||||
Intent intent) {
|
||||
/**
|
||||
* True when the setting is available for the device.
|
||||
*/
|
||||
final boolean mIsDeviceSupported;
|
||||
|
||||
/**
|
||||
* @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);
|
||||
}
|
@@ -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) >
|
||||
*/
|
||||
public final Map<Integer, Boolean> valueMap;
|
||||
|
||||
public InlineSwitchPayload(String newUri, @SettingsSource int settingsSource,
|
||||
Map<Integer, Boolean> map, Intent intent) {
|
||||
super(newUri, PayloadType.INLINE_SWITCH, settingsSource, intent);
|
||||
valueMap = map;
|
||||
/**
|
||||
* 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)
|
||||
*/
|
||||
private boolean mIsStandard;
|
||||
|
||||
/**
|
||||
*
|
||||
* @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;
|
||||
}
|
||||
}
|
||||
|
@@ -52,13 +52,14 @@ public class InlineSwitchViewHolder extends SearchViewHolder {
|
||||
return;
|
||||
}
|
||||
final InlineSwitchPayload payload = (InlineSwitchPayload) result.payload;
|
||||
switchView.setChecked(payload.getSwitchValue(mContext));
|
||||
switchView.setChecked(payload.getValue(mContext) == InlineSwitchPayload.TRUE);
|
||||
switchView.setOnCheckedChangeListener((buttonView, isChecked) -> {
|
||||
final Pair<Integer, Object> value = Pair.create(
|
||||
MetricsEvent.FIELD_SETTINGS_SEARCH_INLINE_RESULT_VALUE, isChecked
|
||||
? 1L : 0L);
|
||||
fragment.onSearchResultClicked(this, payload.settingsUri, value);
|
||||
payload.setSwitchValue(mContext, isChecked);
|
||||
fragment.onSearchResultClicked(this, payload.mSettingKey, value);
|
||||
int newValue = isChecked ? InlineSwitchPayload.TRUE : InlineSwitchPayload.FALSE;
|
||||
payload.setValue(mContext, newValue);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@@ -57,6 +57,37 @@ public class ResultPayload implements Parcelable {
|
||||
int SAVED_QUERY = 3;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enumerates the possible values for the Availability of a setting.
|
||||
*/
|
||||
@IntDef({Availability.AVAILABLE,
|
||||
Availability.DISABLED_DEPENDENCY,
|
||||
Availability.DISABLED_UNSUPPORTED,
|
||||
Availability.RESOURCE_CONTENTION})
|
||||
@Retention(RetentionPolicy.SOURCE)
|
||||
public @interface Availability {
|
||||
/**
|
||||
* The setting is available.
|
||||
*/
|
||||
int AVAILABLE = 0;
|
||||
|
||||
/**
|
||||
* The setting has a dependency which is currently disabled, blocking access.
|
||||
*/
|
||||
int DISABLED_DEPENDENCY = 1;
|
||||
|
||||
/**
|
||||
* The setting is not supported by the device.
|
||||
*/
|
||||
int DISABLED_UNSUPPORTED = 2;
|
||||
|
||||
/**
|
||||
* The setting you are trying to change is being used by another application and cannot
|
||||
* be changed until it is released by said application.
|
||||
*/
|
||||
int RESOURCE_CONTENTION = 3;
|
||||
}
|
||||
|
||||
@IntDef({SettingsSource.UNKNOWN, SettingsSource.SYSTEM, SettingsSource.SECURE,
|
||||
SettingsSource.GLOBAL})
|
||||
@Retention(RetentionPolicy.SOURCE)
|
||||
|
Reference in New Issue
Block a user