Skip to content
This repository has been archived by the owner on Feb 26, 2023. It is now read-only.

@PreferenceChange more parameter types #1399

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,14 @@
* <ul>
* <li>A {@link android.preference.Preference Preference} parameter to know
* which preference was targeted by this event</li>
* <li>An {@link Object} or {@link java.util.Set Set of strings} or
* {@link Boolean} or {@link String} to obtain the new value of the
* {@link android.preference.Preference Preference}</li>
* <li>An {@link Object}, {@link String}, {@link java.util.Set Set of strings}
* and also a {@link Boolean}, {@link Float}, {@link Integer}, {@link Long} or
* their corresponding primitive types to obtain the new value of the
* {@link android.preference.Preference Preference}. Please note with number
* types, we assume that the <code>newValue</code> parameter coming from the
* {@link android.preference.Preference Preference} is a {@link String}, so we
* parse it to a number object (Android {@link android.preference.Preference
* Preference} classes use {@link String}s instead of number objects).</li>
* </ul>
* <blockquote>
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import com.sun.codemodel.JInvocation;
import com.sun.codemodel.JMethod;
import com.sun.codemodel.JMod;
import com.sun.codemodel.JType;
import com.sun.codemodel.JVar;

public class PreferenceChangeHandler extends AbstractPreferenceListenerHandler {
Expand All @@ -59,7 +60,7 @@ public void validate(Element element, AnnotationElements validatedElements, IsVa

validatorHelper.param.hasNoOtherParameterThanPreferenceOrObjectOrSetOrStringOrBoolean(executableElement, valid);
validatorHelper.param.hasZeroOrOnePreferenceParameter(executableElement, valid);
validatorHelper.param.hasAtMostOneStringOrSetOrBooleanOrObjectParameter(executableElement, valid);
validatorHelper.param.hasAtMostOnePreferenceChangeSupportedParameter(executableElement, valid);
}

@Override
Expand All @@ -79,13 +80,24 @@ protected void processParameters(HasPreferences holder, JMethod listenerMethod,
JVar newValueParam = listenerMethod.param(classes().OBJECT, "newValue");

for (VariableElement variableElement : userParameters) {
if (variableElement.asType().toString().equals(CanonicalNameConstants.PREFERENCE)) {
String type = variableElement.asType().toString();

if (type.equals(CanonicalNameConstants.PREFERENCE)) {
call.arg(preferenceParam);
} else if (variableElement.asType().toString().equals(CanonicalNameConstants.OBJECT)) {
} else if (type.equals(CanonicalNameConstants.OBJECT)) {
call.arg(newValueParam);
} else if (type.equals(CanonicalNameConstants.INTEGER) || type.equals(int.class.getName()) || //
type.equals(CanonicalNameConstants.FLOAT) || type.equals(float.class.getName()) || //
type.equals(CanonicalNameConstants.LONG) || type.equals(long.class.getName())) {
JClass wrapperClass = type.startsWith("java") ? holder.refClass(type) : JType.parse(holder.codeModel(), type.replace(".class", "")).boxify();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is quite hard to understand. Is there another way to get the wrapper class ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is that there are two cases:
type is wrapper
type is primitive

In the first case, we can just the holder.refClass. Unfortunetaly in the second case, holder.refClass returns a low letter int class for example. JType.parse is the way to get correct primitive types, but it only expects the name, without the .class. There is no way to get "int" name from "TypeMirror", so we have to remove ".class" manually.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK then.

call.arg(wrapperClass.staticInvoke("valueOf").arg(JExpr.cast(holder.classes().STRING, newValueParam)));
} else {
JClass userParamClass = codeModelHelper.typeMirrorToJClass(variableElement.asType(), holder);
call.arg(JExpr.cast(userParamClass, newValueParam));

if (type.equals(CanonicalNameConstants.STRING_SET)) {
codeModelHelper.addSuppressWarnings(listenerMethod, "unchecked");
}
}
}
}
Expand All @@ -104,5 +116,4 @@ protected String getSetterName() {
protected JClass getListenerClass() {
return classes().PREFERENCE_CHANGE_LISTENER;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public final class CanonicalNameConstants {
public static final String SQL_EXCEPTION = SQLException.class.getCanonicalName();
public static final String INTEGER = Integer.class.getCanonicalName();
public static final String BOOLEAN = Boolean.class.getCanonicalName();
public static final String FLOAT = Float.class.getCanonicalName();
public static final String LONG = Long.class.getCanonicalName();
public static final String ARRAYLIST = ArrayList.class.getCanonicalName();
public static final String SERIALIZABLE = Serializable.class.getCanonicalName();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ public class ValidatorParameterHelper {

private static final List<String> ANDROID_SHERLOCK_MENU_ITEM_QUALIFIED_NAMES = asList(CanonicalNameConstants.MENU_ITEM, CanonicalNameConstants.SHERLOCK_MENU_ITEM);
private static final List<String> EDITOR_ACTION_ALLOWED_PARAMETER_TYPES = asList(CanonicalNameConstants.TEXT_VIEW, CanonicalNameConstants.INTEGER, "int", CanonicalNameConstants.KEY_EVENT);
private static final List<String> PREFERENCE_CHANGE_ALLOWED_NEWVALUE_PARAM = asList(CanonicalNameConstants.OBJECT, CanonicalNameConstants.SET, CanonicalNameConstants.STRING, CanonicalNameConstants.BOOLEAN);
private static final List<String> PREFERENCE_CHANGE_ALLOWED_NEWVALUE_PARAM = asList(CanonicalNameConstants.OBJECT, CanonicalNameConstants.STRING_SET, CanonicalNameConstants.STRING, //
CanonicalNameConstants.BOOLEAN, boolean.class.getName(), //
CanonicalNameConstants.INTEGER, int.class.getName(), //
CanonicalNameConstants.LONG, long.class.getName(), //
CanonicalNameConstants.FLOAT, float.class.getName());

protected final TargetAnnotationHelper annotationHelper;

Expand Down Expand Up @@ -264,7 +268,7 @@ public void hasAtMostOneKeyEventParameter(ExecutableElement executableElement, I

}

public void hasAtMostOneStringOrSetOrBooleanOrObjectParameter(ExecutableElement executableElement, IsValid valid) {
public void hasAtMostOnePreferenceChangeSupportedParameter(ExecutableElement executableElement, IsValid valid) {
hasAtMostOneSpecificParameter(executableElement, PREFERENCE_CHANGE_ALLOWED_NEWVALUE_PARAM, valid);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
<string name="prefStringKey">stringKey</string>

<string name="conventionKey">conventionKey</string>
<string name="editTextPrefKey">editTextPrefKey</string>
<string name="listPreferenceKey">listPreferenceKey</string>
<string name="checkBoxPrefKey">checkBoxPrefKey</string>
<string name="switchPrefKey">switchPrefKey</string>
Expand Down
3 changes: 3 additions & 0 deletions AndroidAnnotations/functional-test-1-5/res/xml/settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
<EditTextPreference
android:key="@string/conventionKey" />

<EditTextPreference
android:key="@string/editTextPrefKey" />

<CheckBoxPreference android:key="@string/checkBoxPrefKey"/>

<SwitchPreference android:key="@string/switchPrefKey"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@
public class PreferenceEventsHandledActivity extends PreferenceActivity {

boolean conventionPrefChanged;
int preferenceChangedParsedValue;
boolean preferenceWithKeyChanged;
Preference preference;
Preference editTextPreference;
Boolean newValue;
boolean conventionPrefClicked;
boolean preferenceWithKeyClicked;
Expand All @@ -52,6 +54,12 @@ void listPreferenceChanged() {
preferenceWithKeyChanged = true;
}

@PreferenceChange(R.string.editTextPrefKey)
void editTextPreferenceChanged(Preference preference, int a) {
preferenceChangedParsedValue = a;
editTextPreference = preference;
}

@PreferenceChange(R.string.checkBoxPrefKey)
void checkBoxPreferenceChanged(Preference preference, Boolean newValue) {
this.preference = preference;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@ public void testPreferenceChangeParameterPassed() {
assertThat(activity.newValue).isSameAs(newValue);
}

@Test
public void testPreferenceChangeParsedParameterPassed() {
Preference preference = activity.findPreference(activity.getString(R.string.editTextPrefKey));
preference.getOnPreferenceChangeListener().onPreferenceChange(preference, "2");

assertThat(activity.editTextPreference).isSameAs(preference);
assertThat(activity.preferenceChangedParsedValue).isEqualTo(2);
}

@Test
public void testPreferenceChangeDefaultReturnValue() {
Preference preference = activity.findPreference(activity.getString(R.string.listPreferenceKey));
Expand Down