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
This commit is contained in:
@@ -19,15 +19,12 @@ import android.provider.Settings;
|
|||||||
import android.support.v14.preference.SwitchPreference;
|
import android.support.v14.preference.SwitchPreference;
|
||||||
import android.support.v7.preference.Preference;
|
import android.support.v7.preference.Preference;
|
||||||
|
|
||||||
import android.util.ArrayMap;
|
|
||||||
import com.android.settings.core.PreferenceController;
|
import com.android.settings.core.PreferenceController;
|
||||||
import com.android.settings.search.DatabaseIndexingUtils;
|
import com.android.settings.search.DatabaseIndexingUtils;
|
||||||
import com.android.settings.search.InlineSwitchPayload;
|
import com.android.settings.search.InlineSwitchPayload;
|
||||||
import com.android.settings.search.ResultPayload;
|
import com.android.settings.search.ResultPayload;
|
||||||
import com.android.settings.R;
|
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;
|
||||||
import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
|
import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
|
||||||
import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL;
|
import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL;
|
||||||
@@ -71,15 +68,12 @@ public class AutoBrightnessPreferenceController extends PreferenceController imp
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ResultPayload getResultPayload() {
|
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,
|
final Intent intent = DatabaseIndexingUtils.buildSubsettingIntent(mContext,
|
||||||
getClass().getName(), mAutoBrightnessKey,
|
getClass().getName(), mAutoBrightnessKey,
|
||||||
mContext.getString(R.string.display_settings));
|
mContext.getString(R.string.display_settings));
|
||||||
|
|
||||||
return new InlineSwitchPayload(SCREEN_BRIGHTNESS_MODE,
|
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 Drawable} icon
|
||||||
* - {@link ResultPayload} payload
|
* - {@link ResultPayload} payload
|
||||||
*/
|
*/
|
||||||
class CursorToSearchResultConverter {
|
public class CursorToSearchResultConverter {
|
||||||
|
|
||||||
private final String TAG = "CursorConverter";
|
private static final String TAG = "CursorConverter";
|
||||||
|
|
||||||
private final Context mContext;
|
private final Context mContext;
|
||||||
|
|
||||||
@@ -103,6 +103,23 @@ class CursorToSearchResultConverter {
|
|||||||
return results;
|
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,
|
private SearchResult buildSingleSearchResultFromCursor(SiteMapManager sitemapManager,
|
||||||
Map<String, Context> contextMap, Cursor cursor, int baseRank) {
|
Map<String, Context> contextMap, Cursor cursor, int baseRank) {
|
||||||
final int docId = cursor.getInt(COLUMN_INDEX_ID);
|
final int docId = cursor.getInt(COLUMN_INDEX_ID);
|
||||||
@@ -162,22 +179,6 @@ class CursorToSearchResultConverter {
|
|||||||
return icon;
|
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) {
|
private List<String> getBreadcrumbs(SiteMapManager siteMapManager, Cursor cursor) {
|
||||||
final String screenTitle = cursor.getString(COLUMN_INDEX_SCREEN_TITLE);
|
final String screenTitle = cursor.getString(COLUMN_INDEX_SCREEN_TITLE);
|
||||||
final String screenClass = cursor.getString(COLUMN_INDEX_CLASS_NAME);
|
final String screenClass = cursor.getString(COLUMN_INDEX_CLASS_NAME);
|
||||||
|
@@ -803,6 +803,7 @@ public class DatabaseIndexingManager {
|
|||||||
entries = XmlParserUtils.getDataEntries(context, attrs);
|
entries = XmlParserUtils.getDataEntries(context, attrs);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO (b/62254931) index primitives instead of payload
|
||||||
payload = DatabaseIndexingUtils.getPayloadFromUriMap(controllerUriMap, key);
|
payload = DatabaseIndexingUtils.getPayloadFromUriMap(controllerUriMap, key);
|
||||||
childFragment = XmlParserUtils.getDataChildFragment(context, attrs);
|
childFragment = XmlParserUtils.getDataChildFragment(context, attrs);
|
||||||
|
|
||||||
|
@@ -19,30 +19,93 @@ package com.android.settings.search;
|
|||||||
|
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.os.Parcel;
|
||||||
|
import com.android.internal.annotations.VisibleForTesting;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Abstract Payload for inline settings results.
|
* Abstract Payload for inline settings results.
|
||||||
*/
|
*/
|
||||||
public abstract class InlinePayload extends ResultPayload {
|
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.
|
* The UI type for the inline result.
|
||||||
*/
|
*/
|
||||||
@PayloadType public int inlineType;
|
@PayloadType final int mInlineType;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Defines where the Setting is stored.
|
* 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);
|
super(intent);
|
||||||
settingsUri = uri;
|
mSettingKey = key;
|
||||||
inlineType = type;
|
mInlineType = payloadType;
|
||||||
settingSource = source;
|
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.os.Parcelable;
|
||||||
import android.provider.Settings;
|
import android.provider.Settings;
|
||||||
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Payload for inline Switch results. Mappings from integer to boolean.
|
* Payload for inline Switch results. Mappings from integer to boolean.
|
||||||
*/
|
*/
|
||||||
public class InlineSwitchPayload extends InlinePayload {
|
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) {
|
* Provides a mapping for how switches are stored.
|
||||||
super(newUri, PayloadType.INLINE_SWITCH, settingsSource, intent);
|
* If mIsStandard is true, then (0 == false) and (1 == true)
|
||||||
valueMap = map;
|
* 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) {
|
private InlineSwitchPayload(Parcel in) {
|
||||||
super(in.readString() /* Uri */ , in.readInt() /* Payload Type */,
|
super(in);
|
||||||
in.readInt() /* Settings Source */,
|
mIsStandard = in.readInt() == TRUE;
|
||||||
(Intent) in.readParcelable(Intent.class.getClassLoader()) /* Intent */);
|
|
||||||
valueMap = in.readHashMap(Integer.class.getClassLoader());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getType() {
|
public int getType() {
|
||||||
return inlineType;
|
return mInlineType;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -63,11 +67,8 @@ public class InlineSwitchPayload extends InlinePayload {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void writeToParcel(Parcel dest, int flags) {
|
public void writeToParcel(Parcel dest, int flags) {
|
||||||
dest.writeString(settingsUri);
|
super.writeToParcel(dest, flags);
|
||||||
dest.writeInt(inlineType);
|
dest.writeInt(mIsStandard ? TRUE : FALSE);
|
||||||
dest.writeInt(settingSource);
|
|
||||||
dest.writeParcelable(mIntent, flags);
|
|
||||||
dest.writeMap(valueMap);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static final Parcelable.Creator<InlineSwitchPayload> CREATOR =
|
public static final Parcelable.Creator<InlineSwitchPayload> CREATOR =
|
||||||
@@ -83,71 +84,65 @@ public class InlineSwitchPayload extends InlinePayload {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
public boolean getSwitchValue(Context context) {
|
@Override
|
||||||
if (valueMap == null) {
|
public int getValue(Context context) {
|
||||||
throw new IllegalStateException("Value map is null");
|
|
||||||
}
|
|
||||||
|
|
||||||
int settingsValue = -1;
|
int settingsValue = -1;
|
||||||
switch(settingSource) {
|
switch(mSettingSource) {
|
||||||
case SettingsSource.SECURE:
|
case SettingsSource.SECURE:
|
||||||
settingsValue = Settings.Secure.getInt(context.getContentResolver(),
|
settingsValue = Settings.Secure.getInt(context.getContentResolver(),
|
||||||
settingsUri, 0);
|
mSettingKey, -1);
|
||||||
break;
|
break;
|
||||||
case SettingsSource.SYSTEM:
|
case SettingsSource.SYSTEM:
|
||||||
settingsValue = Settings.System.getInt(context.getContentResolver(),
|
settingsValue = Settings.System.getInt(context.getContentResolver(),
|
||||||
settingsUri, 0);
|
mSettingKey, -1);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case SettingsSource.GLOBAL:
|
case SettingsSource.GLOBAL:
|
||||||
settingsValue = Settings.Global.getInt(context.getContentResolver(),
|
settingsValue = Settings.Global.getInt(context.getContentResolver(),
|
||||||
settingsUri, 0);
|
mSettingKey, -1);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (settingsValue == -1) {
|
if (settingsValue == -1) {
|
||||||
throw new IllegalStateException("Unable to find setting from uri: "
|
throw new IllegalStateException("Unable to find setting from uri: "
|
||||||
+ settingsUri.toString());
|
+ mSettingKey.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
for (Integer key : valueMap.keySet()) {
|
settingsValue = standardizeInput(settingsValue);
|
||||||
if ((key == settingsValue)) {
|
|
||||||
return valueMap.get(key);
|
return settingsValue;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new IllegalStateException("No results matched the key: " + settingsValue);
|
@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);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setSwitchValue(Context context, boolean isChecked) {
|
newValue = standardizeInput(newValue);
|
||||||
if (valueMap == null) {
|
|
||||||
throw new IllegalStateException("Value map is null");
|
|
||||||
}
|
|
||||||
int switchValue = -1;
|
|
||||||
|
|
||||||
for (Map.Entry<Integer, Boolean> pair : valueMap.entrySet()) {
|
switch(mSettingSource) {
|
||||||
if (pair.getValue() == isChecked) {
|
|
||||||
switchValue = pair.getKey();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (switchValue == -1) {
|
|
||||||
throw new IllegalStateException("Switch value is not set");
|
|
||||||
}
|
|
||||||
|
|
||||||
switch(settingSource) {
|
|
||||||
case SettingsSource.GLOBAL:
|
case SettingsSource.GLOBAL:
|
||||||
Settings.Global.putInt(context.getContentResolver(), settingsUri, switchValue);
|
return Settings.Global.putInt(context.getContentResolver(), mSettingKey, newValue);
|
||||||
return;
|
|
||||||
case SettingsSource.SECURE:
|
case SettingsSource.SECURE:
|
||||||
Settings.Secure.putInt(context.getContentResolver(), settingsUri, switchValue);
|
return Settings.Secure.putInt(context.getContentResolver(), mSettingKey, newValue);
|
||||||
return;
|
|
||||||
case SettingsSource.SYSTEM:
|
case SettingsSource.SYSTEM:
|
||||||
Settings.System.putInt(context.getContentResolver(), settingsUri, switchValue);
|
return Settings.System.putInt(context.getContentResolver(), mSettingKey, newValue);
|
||||||
return;
|
|
||||||
case SettingsSource.UNKNOWN:
|
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;
|
return;
|
||||||
}
|
}
|
||||||
final InlineSwitchPayload payload = (InlineSwitchPayload) result.payload;
|
final InlineSwitchPayload payload = (InlineSwitchPayload) result.payload;
|
||||||
switchView.setChecked(payload.getSwitchValue(mContext));
|
switchView.setChecked(payload.getValue(mContext) == InlineSwitchPayload.TRUE);
|
||||||
switchView.setOnCheckedChangeListener((buttonView, isChecked) -> {
|
switchView.setOnCheckedChangeListener((buttonView, isChecked) -> {
|
||||||
final Pair<Integer, Object> value = Pair.create(
|
final Pair<Integer, Object> value = Pair.create(
|
||||||
MetricsEvent.FIELD_SETTINGS_SEARCH_INLINE_RESULT_VALUE, isChecked
|
MetricsEvent.FIELD_SETTINGS_SEARCH_INLINE_RESULT_VALUE, isChecked
|
||||||
? 1L : 0L);
|
? 1L : 0L);
|
||||||
fragment.onSearchResultClicked(this, payload.settingsUri, value);
|
fragment.onSearchResultClicked(this, payload.mSettingKey, value);
|
||||||
payload.setSwitchValue(mContext, isChecked);
|
int newValue = isChecked ? InlineSwitchPayload.TRUE : InlineSwitchPayload.FALSE;
|
||||||
|
payload.setValue(mContext, newValue);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -57,6 +57,37 @@ public class ResultPayload implements Parcelable {
|
|||||||
int SAVED_QUERY = 3;
|
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,
|
@IntDef({SettingsSource.UNKNOWN, SettingsSource.SYSTEM, SettingsSource.SECURE,
|
||||||
SettingsSource.GLOBAL})
|
SettingsSource.GLOBAL})
|
||||||
@Retention(RetentionPolicy.SOURCE)
|
@Retention(RetentionPolicy.SOURCE)
|
||||||
|
@@ -16,12 +16,15 @@
|
|||||||
|
|
||||||
package com.android.settings.display;
|
package com.android.settings.display;
|
||||||
|
|
||||||
|
import android.content.ContentResolver;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.provider.Settings;
|
import android.provider.Settings;
|
||||||
import com.android.settings.SettingsRobolectricTestRunner;
|
import com.android.settings.SettingsRobolectricTestRunner;
|
||||||
import com.android.settings.TestConfig;
|
import com.android.settings.TestConfig;
|
||||||
|
import com.android.settings.search.InlinePayload;
|
||||||
import com.android.settings.search.InlineSwitchPayload;
|
import com.android.settings.search.InlineSwitchPayload;
|
||||||
import com.android.settings.search.ResultPayload;
|
import com.android.settings.search.ResultPayload;
|
||||||
|
import com.android.settings.search.ResultPayload.Availability;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
@@ -78,13 +81,25 @@ public class AutoBrightnessPreferenceControllerTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testPreferenceController_CorrectPayload() {
|
public void testSetValue_updatesCorrectly() {
|
||||||
final Context context = ShadowApplication.getInstance().getApplicationContext();
|
int newValue = 1;
|
||||||
mController = new AutoBrightnessPreferenceController(context, PREFERENCE_KEY);
|
ContentResolver resolver = mContext.getContentResolver();
|
||||||
InlineSwitchPayload payload = (InlineSwitchPayload) mController.getResultPayload();
|
Settings.System.putInt(resolver, SCREEN_BRIGHTNESS_MODE, 0);
|
||||||
assertThat(payload.settingsUri).isEqualTo("screen_brightness_mode");
|
|
||||||
assertThat(payload.settingSource).isEqualTo(ResultPayload.SettingsSource.SYSTEM);
|
((InlinePayload) mController.getResultPayload()).setValue(mContext, newValue);
|
||||||
assertThat(payload.valueMap.get(1)).isEqualTo(true);
|
int updatedValue = Settings.System.getInt(resolver, SCREEN_BRIGHTNESS_MODE, -1);
|
||||||
assertThat(payload.valueMap.get(0)).isEqualTo(false);
|
|
||||||
|
assertThat(updatedValue).isEqualTo(newValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetValue_correctValueReturned() {
|
||||||
|
int currentValue = 1;
|
||||||
|
ContentResolver resolver = mContext.getContentResolver();
|
||||||
|
Settings.System.putInt(resolver, SCREEN_BRIGHTNESS_MODE, currentValue);
|
||||||
|
|
||||||
|
int newValue = ((InlinePayload) mController.getResultPayload()).getValue(mContext);
|
||||||
|
|
||||||
|
assertThat(newValue).isEqualTo(currentValue);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -23,7 +23,6 @@ import android.content.Intent;
|
|||||||
import android.database.Cursor;
|
import android.database.Cursor;
|
||||||
import android.database.MatrixCursor;
|
import android.database.MatrixCursor;
|
||||||
import android.graphics.drawable.Drawable;
|
import android.graphics.drawable.Drawable;
|
||||||
import android.util.ArrayMap;
|
|
||||||
|
|
||||||
import com.android.settings.DisplaySettings;
|
import com.android.settings.DisplaySettings;
|
||||||
import com.android.settings.R;
|
import com.android.settings.R;
|
||||||
@@ -31,14 +30,9 @@ import com.android.settings.SettingsRobolectricTestRunner;
|
|||||||
import com.android.settings.TestConfig;
|
import com.android.settings.TestConfig;
|
||||||
import com.android.settings.dashboard.SiteMapManager;
|
import com.android.settings.dashboard.SiteMapManager;
|
||||||
import com.android.settings.gestures.SwipeToNotificationSettings;
|
import com.android.settings.gestures.SwipeToNotificationSettings;
|
||||||
import com.android.settings.search.CursorToSearchResultConverter;
|
import com.android.settings.search.ResultPayload.Availability;
|
||||||
import com.android.settings.search.DatabaseResultLoader;
|
|
||||||
import com.android.settings.search.InlineSwitchPayload;
|
|
||||||
import com.android.settings.search.ResultPayload;
|
|
||||||
import com.android.settings.search.ResultPayload.PayloadType;
|
import com.android.settings.search.ResultPayload.PayloadType;
|
||||||
|
|
||||||
import com.android.settings.search.ResultPayloadUtils;
|
|
||||||
import com.android.settings.search.SearchResult;
|
|
||||||
import com.android.settings.wifi.WifiSettings;
|
import com.android.settings.wifi.WifiSettings;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
@@ -221,14 +215,12 @@ public class CursorToSearchResultConverterTest {
|
|||||||
final String uri = "test.com";
|
final String uri = "test.com";
|
||||||
final int type = ResultPayload.PayloadType.INLINE_SWITCH;
|
final int type = ResultPayload.PayloadType.INLINE_SWITCH;
|
||||||
final int source = ResultPayload.SettingsSource.SECURE;
|
final int source = ResultPayload.SettingsSource.SECURE;
|
||||||
final ArrayMap<Integer, Boolean> map = new ArrayMap<>();
|
|
||||||
map.put(1, true);
|
|
||||||
map.put(0, false);
|
|
||||||
final String intentKey = "key";
|
final String intentKey = "key";
|
||||||
final String intentVal = "value";
|
final String intentVal = "value";
|
||||||
final Intent intent = new Intent();
|
final Intent intent = new Intent();
|
||||||
intent.putExtra(intentKey, intentVal);
|
intent.putExtra(intentKey, intentVal);
|
||||||
final InlineSwitchPayload payload = new InlineSwitchPayload(uri, source, map, intent);
|
final InlineSwitchPayload payload = new InlineSwitchPayload(uri, source, 1 /* onValue */,
|
||||||
|
intent, true /* isDeviceSupported */);
|
||||||
|
|
||||||
cursor.addRow(new Object[]{
|
cursor.addRow(new Object[]{
|
||||||
KEY.hashCode(), // Doc ID
|
KEY.hashCode(), // Doc ID
|
||||||
@@ -251,11 +243,11 @@ public class CursorToSearchResultConverterTest {
|
|||||||
for (SearchResult result : results) {
|
for (SearchResult result : results) {
|
||||||
final InlineSwitchPayload newPayload = (InlineSwitchPayload) result.payload;
|
final InlineSwitchPayload newPayload = (InlineSwitchPayload) result.payload;
|
||||||
final Intent rebuiltIntent = newPayload.getIntent();
|
final Intent rebuiltIntent = newPayload.getIntent();
|
||||||
assertThat(newPayload.settingsUri).isEqualTo(uri);
|
assertThat(newPayload.mSettingKey).isEqualTo(uri);
|
||||||
assertThat(newPayload.inlineType).isEqualTo(type);
|
assertThat(newPayload.mInlineType).isEqualTo(type);
|
||||||
assertThat(newPayload.settingSource).isEqualTo(source);
|
assertThat(newPayload.mSettingSource).isEqualTo(source);
|
||||||
assertThat(newPayload.valueMap.get(1)).isTrue();
|
assertThat(newPayload.isStandard()).isTrue();
|
||||||
assertThat(newPayload.valueMap.get(0)).isFalse();
|
assertThat(newPayload.getAvailability()).isEqualTo(Availability.AVAILABLE);
|
||||||
assertThat(rebuiltIntent.getStringExtra(intentKey)).isEqualTo(intentVal);
|
assertThat(rebuiltIntent.getStringExtra(intentKey)).isEqualTo(intentVal);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1089,7 +1089,6 @@ public class DatabaseIndexingManagerTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void insertSpecialCase(String specialCase, boolean enabled, String key) {
|
private void insertSpecialCase(String specialCase, boolean enabled, String key) {
|
||||||
|
|
||||||
ContentValues values = new ContentValues();
|
ContentValues values = new ContentValues();
|
||||||
values.put(IndexDatabaseHelper.IndexColumns.DOCID, specialCase.hashCode());
|
values.put(IndexDatabaseHelper.IndexColumns.DOCID, specialCase.hashCode());
|
||||||
values.put(IndexDatabaseHelper.IndexColumns.LOCALE, localeStr);
|
values.put(IndexDatabaseHelper.IndexColumns.LOCALE, localeStr);
|
||||||
|
@@ -26,17 +26,12 @@ import com.android.settings.TestConfig;
|
|||||||
import com.android.settings.search.DatabaseIndexingManager.DatabaseRow;
|
import com.android.settings.search.DatabaseIndexingManager.DatabaseRow;
|
||||||
import com.android.settings.search.DatabaseIndexingManager.DatabaseRow.Builder;
|
import com.android.settings.search.DatabaseIndexingManager.DatabaseRow.Builder;
|
||||||
|
|
||||||
import com.android.settings.search.InlineSwitchPayload;
|
|
||||||
import com.android.settings.search.ResultPayload;
|
|
||||||
import com.android.settings.search.ResultPayloadUtils;
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.robolectric.RuntimeEnvironment;
|
import org.robolectric.RuntimeEnvironment;
|
||||||
import org.robolectric.annotation.Config;
|
import org.robolectric.annotation.Config;
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
|
|
||||||
import static com.google.common.truth.Truth.assertThat;
|
import static com.google.common.truth.Truth.assertThat;
|
||||||
|
|
||||||
@RunWith(SettingsRobolectricTestRunner.class)
|
@RunWith(SettingsRobolectricTestRunner.class)
|
||||||
@@ -118,15 +113,15 @@ public class DatabaseRowTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testRowWithInlinePayload_genericPayloadNotAdded() {
|
public void testRowWithInlinePayload_genericPayloadNotAdded() {
|
||||||
final String URI = "test uri";
|
final String URI = "test uri";
|
||||||
final InlineSwitchPayload payload = new InlineSwitchPayload(URI, 0,
|
final InlineSwitchPayload payload = new InlineSwitchPayload(URI, 0 /* mSettingSource */,
|
||||||
new HashMap<Integer, Boolean>(), null);
|
1 /* onValue */, null /* intent */, true /* isDeviceSupported */);
|
||||||
mBuilder.setPayload(payload);
|
mBuilder.setPayload(payload);
|
||||||
final DatabaseRow row = generateRow();
|
final DatabaseRow row = generateRow();
|
||||||
final InlineSwitchPayload unmarshalledPayload = ResultPayloadUtils
|
final InlineSwitchPayload unmarshalledPayload = ResultPayloadUtils
|
||||||
.unmarshall(row.payload, InlineSwitchPayload.CREATOR);
|
.unmarshall(row.payload, InlineSwitchPayload.CREATOR);
|
||||||
|
|
||||||
assertThat(row.payloadType).isEqualTo(ResultPayload.PayloadType.INLINE_SWITCH);
|
assertThat(row.payloadType).isEqualTo(ResultPayload.PayloadType.INLINE_SWITCH);
|
||||||
assertThat(unmarshalledPayload.settingsUri).isEqualTo(URI);
|
assertThat(unmarshalledPayload.mSettingKey).isEqualTo(URI);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -137,8 +132,8 @@ public class DatabaseRowTest {
|
|||||||
final Intent intent = new Intent();
|
final Intent intent = new Intent();
|
||||||
intent.setComponent(component);
|
intent.setComponent(component);
|
||||||
|
|
||||||
final InlineSwitchPayload payload = new InlineSwitchPayload(URI, 0,
|
final InlineSwitchPayload payload = new InlineSwitchPayload(URI, 0 /* mSettingSource */,
|
||||||
new HashMap<Integer, Boolean>(), intent);
|
1 /* onValue */, intent, true /* isDeviceSupported */);
|
||||||
mBuilder.setPayload(payload);
|
mBuilder.setPayload(payload);
|
||||||
final DatabaseRow row = generateRow();
|
final DatabaseRow row = generateRow();
|
||||||
final InlineSwitchPayload unmarshalledPayload = ResultPayloadUtils
|
final InlineSwitchPayload unmarshalledPayload = ResultPayloadUtils
|
||||||
|
@@ -17,76 +17,57 @@
|
|||||||
|
|
||||||
package com.android.settings.search;
|
package com.android.settings.search;
|
||||||
|
|
||||||
|
import android.content.ContentResolver;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.os.Parcel;
|
import android.os.Parcel;
|
||||||
import android.util.ArrayMap;
|
import android.provider.Settings;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
|
||||||
import com.android.settings.SettingsRobolectricTestRunner;
|
import com.android.settings.SettingsRobolectricTestRunner;
|
||||||
import com.android.settings.TestConfig;
|
import com.android.settings.TestConfig;
|
||||||
|
import com.android.settings.search.ResultPayload.Availability;
|
||||||
|
import com.android.settings.search.ResultPayload.SettingsSource;
|
||||||
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
|
import org.robolectric.RuntimeEnvironment;
|
||||||
import org.robolectric.annotation.Config;
|
import org.robolectric.annotation.Config;
|
||||||
import org.robolectric.shadows.ShadowApplication;
|
|
||||||
|
|
||||||
|
import static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE;
|
||||||
import static com.google.common.truth.Truth.assertThat;
|
import static com.google.common.truth.Truth.assertThat;
|
||||||
import static org.junit.Assert.fail;
|
|
||||||
|
|
||||||
@RunWith(SettingsRobolectricTestRunner.class)
|
@RunWith(SettingsRobolectricTestRunner.class)
|
||||||
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
|
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
|
||||||
public class InlineSwitchPayloadTest {
|
public class InlineSwitchPayloadTest {
|
||||||
|
|
||||||
@Test
|
private static final String DUMMY_SETTING = "inline_test";
|
||||||
public void testGetSwitch_EmptyMap_ExceptionThrown() {
|
private static final int STANDARD_ON = 1;
|
||||||
final String uri = "test.com";
|
private static final int FLIPPED_ON = 0;
|
||||||
final int source = ResultPayload.SettingsSource.SECURE;
|
|
||||||
|
|
||||||
final Context context = ShadowApplication.getInstance().getApplicationContext();
|
private Context mContext;
|
||||||
InlineSwitchPayload payload = new InlineSwitchPayload(uri, source, null, null);
|
|
||||||
try {
|
|
||||||
payload.getSwitchValue(context);
|
|
||||||
fail("Should have thrown exception for null map");
|
|
||||||
} catch (IllegalStateException e) {
|
|
||||||
assertThat(e).isNotNull();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Before
|
||||||
public void testGetSwitch_BadMap_ExceptionThrown() {
|
public void setUp() {
|
||||||
final String uri = "test.com";
|
mContext = RuntimeEnvironment.application;
|
||||||
final int source = ResultPayload.SettingsSource.SECURE;
|
|
||||||
final ArrayMap<Integer, Boolean> map = new ArrayMap<>();
|
|
||||||
|
|
||||||
final Context context = ShadowApplication.getInstance().getApplicationContext();
|
|
||||||
InlineSwitchPayload payload = new InlineSwitchPayload(uri, source, map, null);
|
|
||||||
try {
|
|
||||||
payload.getSwitchValue(context);
|
|
||||||
fail("Should have thrown exception for bad map");
|
|
||||||
} catch (IllegalStateException e) {
|
|
||||||
assertThat(e).isNotNull();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testConstructor_DataRetained() {
|
public void testConstructor_DataRetained() {
|
||||||
final String uri = "test.com";
|
final String uri = "test.com";
|
||||||
final int type = ResultPayload.PayloadType.INLINE_SWITCH;
|
final int type = ResultPayload.PayloadType.INLINE_SWITCH;
|
||||||
final int source = ResultPayload.SettingsSource.SECURE;
|
final int source = SettingsSource.SECURE;
|
||||||
final ArrayMap<Integer, Boolean> map = new ArrayMap<>();
|
|
||||||
map.put(1, true);
|
|
||||||
map.put(0, false);
|
|
||||||
final String intentKey = "key";
|
final String intentKey = "key";
|
||||||
final String intentVal = "value";
|
final String intentVal = "value";
|
||||||
final Intent intent = new Intent();
|
final Intent intent = new Intent();
|
||||||
intent.putExtra(intentKey, intentVal);
|
intent.putExtra(intentKey, intentVal);
|
||||||
|
|
||||||
InlineSwitchPayload payload = new InlineSwitchPayload(uri, source, map, intent);
|
InlineSwitchPayload payload = new InlineSwitchPayload(uri, source, 1, intent, true);
|
||||||
final Intent retainedIntent = payload.getIntent();
|
final Intent retainedIntent = payload.getIntent();
|
||||||
assertThat(payload.settingsUri).isEqualTo(uri);
|
assertThat(payload.mSettingKey).isEqualTo(uri);
|
||||||
assertThat(payload.inlineType).isEqualTo(type);
|
assertThat(payload.mInlineType).isEqualTo(type);
|
||||||
assertThat(payload.settingSource).isEqualTo(source);
|
assertThat(payload.mSettingSource).isEqualTo(source);
|
||||||
assertThat(payload.valueMap.get(1)).isTrue();
|
assertThat(payload.isStandard()).isTrue();
|
||||||
assertThat(payload.valueMap.get(0)).isFalse();
|
assertThat(payload.getAvailability()).isEqualTo(ResultPayload.Availability.AVAILABLE);
|
||||||
assertThat(retainedIntent.getStringExtra(intentKey)).isEqualTo(intentVal);
|
assertThat(retainedIntent.getStringExtra(intentKey)).isEqualTo(intentVal);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -94,32 +75,150 @@ public class InlineSwitchPayloadTest {
|
|||||||
public void testParcelConstructor_DataRetained() {
|
public void testParcelConstructor_DataRetained() {
|
||||||
String uri = "test.com";
|
String uri = "test.com";
|
||||||
int type = ResultPayload.PayloadType.INLINE_SWITCH;
|
int type = ResultPayload.PayloadType.INLINE_SWITCH;
|
||||||
int source = ResultPayload.SettingsSource.SECURE;
|
int source = SettingsSource.SECURE;
|
||||||
final ArrayMap<Integer, Boolean> map = new ArrayMap<>();
|
|
||||||
map.put(1, true);
|
|
||||||
map.put(0, false);
|
|
||||||
final String intentKey = "key";
|
final String intentKey = "key";
|
||||||
final String intentVal = "value";
|
final String intentVal = "value";
|
||||||
final Intent intent = new Intent();
|
final Intent intent = new Intent();
|
||||||
intent.putExtra(intentKey, intentVal);
|
intent.putExtra(intentKey, intentVal);
|
||||||
|
|
||||||
Parcel parcel = Parcel.obtain();
|
Parcel parcel = Parcel.obtain();
|
||||||
|
parcel.writeParcelable(intent, 0);
|
||||||
parcel.writeString(uri);
|
parcel.writeString(uri);
|
||||||
parcel.writeInt(type);
|
parcel.writeInt(type);
|
||||||
parcel.writeInt(source);
|
parcel.writeInt(source);
|
||||||
parcel.writeParcelable(intent, 0);
|
parcel.writeInt(InlineSwitchPayload.TRUE);
|
||||||
parcel.writeMap(map);
|
parcel.writeInt(InlineSwitchPayload.TRUE);
|
||||||
parcel.setDataPosition(0);
|
parcel.setDataPosition(0);
|
||||||
|
|
||||||
InlineSwitchPayload payload = InlineSwitchPayload.CREATOR.createFromParcel(parcel);
|
InlineSwitchPayload payload = InlineSwitchPayload.CREATOR.createFromParcel(parcel);
|
||||||
final Intent builtIntent = payload.getIntent();
|
final Intent builtIntent = payload.getIntent();
|
||||||
assertThat(payload.settingsUri).isEqualTo(uri);
|
assertThat(payload.mSettingKey).isEqualTo(uri);
|
||||||
assertThat(payload.inlineType).isEqualTo(type);
|
assertThat(payload.mInlineType).isEqualTo(type);
|
||||||
assertThat(payload.settingSource).isEqualTo(source);
|
assertThat(payload.mSettingSource).isEqualTo(source);
|
||||||
assertThat(payload.valueMap.get(1)).isTrue();
|
assertThat(payload.isStandard()).isTrue();
|
||||||
assertThat(payload.valueMap.get(0)).isFalse();
|
assertThat(payload.getAvailability()).isEqualTo(Availability.AVAILABLE);
|
||||||
assertThat(builtIntent.getStringExtra(intentKey)).isEqualTo(intentVal);
|
assertThat(builtIntent.getStringExtra(intentKey)).isEqualTo(intentVal);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetSecure_returnsSecureSetting() {
|
||||||
|
InlineSwitchPayload payload = new InlineSwitchPayload(DUMMY_SETTING, SettingsSource.SECURE,
|
||||||
|
STANDARD_ON, null /* intent */, true);
|
||||||
|
int currentValue = 1;
|
||||||
|
Settings.Secure.putInt(mContext.getContentResolver(), DUMMY_SETTING, currentValue);
|
||||||
|
|
||||||
|
int newValue = payload.getValue(mContext);
|
||||||
|
|
||||||
|
assertThat(newValue).isEqualTo(currentValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetGlobal_returnsGlobalSetting() {
|
||||||
|
InlineSwitchPayload payload = new InlineSwitchPayload(DUMMY_SETTING, SettingsSource.GLOBAL,
|
||||||
|
STANDARD_ON, null /* intent */, true);
|
||||||
|
int currentValue = 1;
|
||||||
|
Settings.Global.putInt(mContext.getContentResolver(), DUMMY_SETTING, currentValue);
|
||||||
|
|
||||||
|
int newValue = payload.getValue(mContext);
|
||||||
|
|
||||||
|
assertThat(newValue).isEqualTo(currentValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetSystem_returnsSystemSetting() {
|
||||||
|
InlineSwitchPayload payload = new InlineSwitchPayload(DUMMY_SETTING, SettingsSource.SYSTEM,
|
||||||
|
STANDARD_ON, null /* intent */, true);
|
||||||
|
int currentValue = 1;
|
||||||
|
Settings.System.putInt(mContext.getContentResolver(), DUMMY_SETTING, currentValue);
|
||||||
|
|
||||||
|
int newValue = payload.getValue(mContext);
|
||||||
|
|
||||||
|
assertThat(newValue).isEqualTo(currentValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSetSecure_updatesSecureSetting() {
|
||||||
|
InlineSwitchPayload payload = new InlineSwitchPayload(DUMMY_SETTING, SettingsSource.SECURE,
|
||||||
|
STANDARD_ON, null /* intent */, true);
|
||||||
|
int newValue = 1;
|
||||||
|
ContentResolver resolver = mContext.getContentResolver();
|
||||||
|
Settings.Secure.putInt(resolver, SCREEN_BRIGHTNESS_MODE, 0);
|
||||||
|
|
||||||
|
payload.setValue(mContext, newValue);
|
||||||
|
int updatedValue = Settings.System.getInt(resolver, DUMMY_SETTING, -1);
|
||||||
|
|
||||||
|
assertThat(updatedValue).isEqualTo(newValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSetGlobal_updatesGlobalSetting() {
|
||||||
|
InlineSwitchPayload payload = new InlineSwitchPayload(DUMMY_SETTING, SettingsSource.GLOBAL,
|
||||||
|
STANDARD_ON, null /* intent */, true);
|
||||||
|
int newValue = 1;
|
||||||
|
ContentResolver resolver = mContext.getContentResolver();
|
||||||
|
Settings.Global.putInt(resolver, SCREEN_BRIGHTNESS_MODE, 0);
|
||||||
|
|
||||||
|
payload.setValue(mContext, newValue);
|
||||||
|
int updatedValue = Settings.Global.getInt(resolver, DUMMY_SETTING, -1);
|
||||||
|
|
||||||
|
assertThat(updatedValue).isEqualTo(newValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSetSystem_updatesSystemSetting() {
|
||||||
|
InlineSwitchPayload payload = new InlineSwitchPayload(DUMMY_SETTING, SettingsSource.SYSTEM,
|
||||||
|
STANDARD_ON, null /* intent */, true);
|
||||||
|
int newValue = 1;
|
||||||
|
ContentResolver resolver = mContext.getContentResolver();
|
||||||
|
Settings.System.putInt(resolver, SCREEN_BRIGHTNESS_MODE, 0);
|
||||||
|
|
||||||
|
payload.setValue(mContext, newValue);
|
||||||
|
int updatedValue = Settings.System.getInt(resolver, DUMMY_SETTING, -1);
|
||||||
|
|
||||||
|
assertThat(updatedValue).isEqualTo(newValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetSystem_flippedSetting_returnsFlippedValue() {
|
||||||
|
// Stores 1s as 0s, and vis versa
|
||||||
|
InlineSwitchPayload payload = new InlineSwitchPayload(DUMMY_SETTING, SettingsSource.SYSTEM,
|
||||||
|
FLIPPED_ON, null /* intent */, true);
|
||||||
|
int currentValue = 1;
|
||||||
|
Settings.System.putInt(mContext.getContentResolver(), DUMMY_SETTING, currentValue);
|
||||||
|
|
||||||
|
int newValue = payload.getValue(mContext);
|
||||||
|
|
||||||
|
assertThat(newValue).isEqualTo(1 - currentValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSetSystem_flippedSetting_updatesToFlippedValue() {
|
||||||
|
// Stores 1s as 0s, and vis versa
|
||||||
|
InlineSwitchPayload payload = new InlineSwitchPayload(DUMMY_SETTING, SettingsSource.SYSTEM,
|
||||||
|
FLIPPED_ON, null /* intent */, true);
|
||||||
|
int newValue = 1;
|
||||||
|
ContentResolver resolver = mContext.getContentResolver();
|
||||||
|
Settings.System.putInt(resolver, SCREEN_BRIGHTNESS_MODE, newValue);
|
||||||
|
|
||||||
|
payload.setValue(mContext, newValue);
|
||||||
|
int updatedValue = Settings.System.getInt(resolver, DUMMY_SETTING, -1);
|
||||||
|
|
||||||
|
assertThat(updatedValue).isEqualTo(1 - newValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = IllegalArgumentException.class)
|
||||||
|
public void testSetSystem_negativeValue_ThrowsError() {
|
||||||
|
InlineSwitchPayload payload = new InlineSwitchPayload(DUMMY_SETTING, SettingsSource.SYSTEM,
|
||||||
|
STANDARD_ON, null /* intent */, true);
|
||||||
|
|
||||||
|
payload.setValue(mContext, -1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = IllegalArgumentException.class)
|
||||||
|
public void testSetSystem_highValue_ThrowsError() {
|
||||||
|
InlineSwitchPayload payload = new InlineSwitchPayload(DUMMY_SETTING, SettingsSource.SYSTEM,
|
||||||
|
STANDARD_ON, null /* intent */, true);
|
||||||
|
|
||||||
|
payload.setValue(mContext, 2);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -86,7 +86,7 @@ public class InlineSwitchViewHolderTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testBindViewElements_AllUpdated() {
|
public void testBindViewElements_AllUpdated() {
|
||||||
when(mPayload.getSwitchValue(any(Context.class))).thenReturn(true);
|
when(mPayload.getValue(any(Context.class))).thenReturn(1);
|
||||||
SearchResult result = getSearchResult();
|
SearchResult result = getSearchResult();
|
||||||
mHolder.onBind(mFragment, result);
|
mHolder.onBind(mFragment, result);
|
||||||
// Precondition: switch is on.
|
// Precondition: switch is on.
|
||||||
@@ -102,10 +102,12 @@ public class InlineSwitchViewHolderTest {
|
|||||||
|
|
||||||
private SearchResult getSearchResult() {
|
private SearchResult getSearchResult() {
|
||||||
SearchResult.Builder builder = new SearchResult.Builder();
|
SearchResult.Builder builder = new SearchResult.Builder();
|
||||||
|
|
||||||
builder.setTitle(TITLE)
|
builder.setTitle(TITLE)
|
||||||
.setSummary(SUMMARY)
|
.setSummary(SUMMARY)
|
||||||
.setRank(1)
|
.setRank(1)
|
||||||
.setPayload(new InlineSwitchPayload("", 0, null, null))
|
.setPayload(new InlineSwitchPayload("" /* uri */, 0 /* mSettingSource */,
|
||||||
|
1 /* onValue */, null /* intent */, true /* isDeviceSupported */))
|
||||||
.addBreadcrumbs(new ArrayList<>())
|
.addBreadcrumbs(new ArrayList<>())
|
||||||
.setIcon(mIcon)
|
.setIcon(mIcon)
|
||||||
.setPayload(mPayload)
|
.setPayload(mPayload)
|
||||||
|
Reference in New Issue
Block a user